Solved

MFC applications allow user to delete open files!!??

Posted on 2003-10-28
7
194 Views
Last Modified: 2013-11-20
I have bult a SDI based application using MFC 6.0. I use the CArchive mechanism to read/write data to filse.  While a document file is open through my application , if I try to delete the file from the Windows Explorer, the file gets deleted. How can I prevent this from happening? This behaviour is also seen in files opened through WordPad / the VC6 IDE. Files currently open from these apps. can be deleted from the Windows Explorer without even a warning! I dont want to allow this behaviour for files currently opened using my application.
0
Comment
Question by:sarah81
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9632682
That is a setting of the system (warn on delete).
What you could do is set the file to be read only after you archive (and set to read/write before you archive).  That should result in the system at least questioning the delete.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9632694
See CFile::GetStatus and SetStatus in the help for more info

CFileStatus status;
CFile::GetStatus( pFileName, status );
status.m_attribute = newAttribute;
CFile::SetStatus( pFileName, status );

where the m_attribute is
enum Attribute {
   normal =    0x00,
   readOnly =  0x01,
   hidden =    0x02,
   system =    0x04,
   volume =    0x08,
   directory = 0x10,
   archive =   0x20
   };

0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9632793
Alternatively, you could just open the file and keep the file open for as long as you are editing. This means that no other process can delete the file - if you set the falgs correctly, you can prevent other processes writing to the file too :

For example

CFile fileWorking("C:\\MyFile.txt", CFile::modeRead | CFile::shareDenyWrite);

will stop other processes writing to the file, or deleting it. It could however be read.

fileWorking.Close();

will allow full access to the file again.

Hope this helps
Martyn
0
 
LVL 1

Author Comment

by:sarah81
ID: 9639141
Neither of these work.

Setting status to read only wont work for me because I can still delete the file from the Windows Explorer. I dont need just a confirmation from the user whether the file should be deleted or not. I want to lock the file so that in case someone tries to delete it, s/he gets error that the file is currently in use.

As for creating a new CFile object for the same file even with shareDenyWrite flag doesnt prevent from deleting the file.

But thanks! this put me on the path to the solution! This is the solution:

Override OnOpenDocument( ) in your CDocument derived class like this:

BOOL CTestDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
   m_pFile = GetFile(lpszPathName, CFile::shareDenyWrite, NULL);
   //CFile temp(lpszPathName, CFile::modeRead|CFile::shareDenyWrite); // This Doesnt Work!!
   return CDocument::OnOpenDocument(lpszPathName);
}

This method returns a pointer to the file, be sure to close the file when you exit from your app.  Now when you try to delete a file opened through your application, the explorer will give you "file in use" error!
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 11885606
Closed, 75 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 22
has22 challenge 11 72
java ^ examples 8 57
Making an alias 7 54
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now