Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 86
Doc'in system (example?) BA 7 105
countEvens challenge 2 97
building a service to accessed via telnet client, want colors ? 4 88
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

816 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

8 Experts available now in Live!

Get 1:1 Help Now