Solved

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

Posted on 2003-10-28
7
203 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 133
fizzArray  challenge 1 95
Problem to open Excel file 15 174
Problem to App source 6 70
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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