Checking whether a file is already in use or not.

Posted on 2003-12-09
Last Modified: 2013-11-20

I am trying to write a rotuine, which will check whether a given file is already in use or not.

For writing this, I am using the routine, OpenFile with OF_SHARE_EXCLUSIVE mode.

When OpenFile is used on a file which is already opened by any other application, then it is returning error message (as it should) which is indicating that the file is already in use.

This seems to work fine only with Binary files.

when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode. Even from windows explorer also, the text based files opened in any other application can be deleted or modified without any problem.

Can someone tell me a way of checking both Text Based files and Binary files which are already in use ?


Question by:gangisetti
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

Accepted Solution

Priyesh earned 100 total points
ID: 9909972
Try this:

      if(hFile == INVALID_HANDLE_VALUE)
            LPVOID lpMsgBuf;
            if (!FormatMessage(
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,
                  NULL ))
               // Handle the error.
               return TRUE ;

            // Process any inserts in lpMsgBuf.
            // ...

            // Display the string.
            AfxMessageBox((LPCTSTR)lpMsgBuf) ;

            // Free the buffer.
            LocalFree( lpMsgBuf ) ;

If any open handles exists to the above file, there will be an OS error.  This is true for all files. Editors might just read the file into memory and then close handle, that's why you could delete the file even when it is open in an editor.

If this does not work for you, you can look into file/ directory watch (NT and higher)
LVL 48

Expert Comment

ID: 9910039
>> when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode.

This happens because Visual Studio doesn't keep opened file handle. It opens file only to load it into memory and to save it. So, your program is OK.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
ORA-01403: no data found 43 112
sumNumber challenge 16 140
Prevent this page from creating additional dialogs. 3 726
java ^ examples 8 70
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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