Solved

Checking whether a file is already in use or not.

Posted on 2003-12-09
5
215 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 84
Generic progress indicator 6 117
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 128
Path of Workbook 3 78
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…
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 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 …

809 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