Solved

Checking whether a file is already in use or not.

Posted on 2003-12-09
5
216 Views
Last Modified: 2013-11-20
Hi,

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 ?

Thanks!
Gangisetti

0
Comment
Question by:gangisetti
5 Comments
 
LVL 9

Accepted Solution

by:
Priyesh earned 100 total points
ID: 9909972
Try this:

      HANDLE hFile = CreateFile("c:\\1.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
      if(hFile == INVALID_HANDLE_VALUE)
      {
            LPVOID lpMsgBuf;
            if (!FormatMessage(
                  FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  GetLastError(),
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,
                  0,
                  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)
0
 
LVL 48

Expert Comment

by:AlexFM
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.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 87
Generic progress indicator 6 117
maxBlock challenge 30 142
NotAlone Challenge 20 84
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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