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

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.
LVL 1
sarah81Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
martynjpearsonCommented:
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
sarah81Author Commented:
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
LunchyCommented:
Closed, 75 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.