Solved

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

Posted on 2003-10-28
7
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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