locked
How do i know a file is opened (either for read or write)??? RRS feed

  • Question

  • Hello all,

    I was trying to delete a file in my application. The file is used in more than one place (for read + write). Now, when i want to delete this file, it sometimes generates an error message "The file is being used by other process". This shows me that the file is still open could be for read or for write operation by other process. Is there any way to check whether or not a file is opened (for read or write) in C# given a filename? 

    Thanks,
    Zolyit
    Tuesday, August 11, 2009 10:14 PM

Answers

  • It is not possible in Windows.  In fact, you shouldn't try.  This is inherent in multitasking operating systems.  Say you have a function called IsFileLocked().  It returns False so you open the file.  This will occasionally fail.  Right between the function return and the file open call, Windows pre-empts your thread and another process opens the file.  Notoriously a virus scanner or the Windows Search indexer.  After a while, your thread resumes and make the file open call.  Kaboom.

    The workaround is simple, just open the file.  If you get an IOException, the file was in use.  If you don't, there's no way that any other thread can steal the file away from you.

    Hans Passant.
    • Proposed as answer by Kenneth Haugland Wednesday, August 12, 2009 3:43 AM
    • Marked as answer by Kira Qian Tuesday, August 18, 2009 8:03 AM
    Wednesday, August 12, 2009 1:43 AM
  • As nobugz said you shouldn't test to see if the file is deletable, you should try to delete it and catch the appropriate exception (i.e System.IOException or System.PermissionDeniedException or whatever is thrown in this case) and then handle the error (i.e do whatever you would have done if you had detected the file was in use).

    If you have code like;

    If IsFileLocked(myfile)
    System.IO.File.Delete(myfile);

    The file could be opened in between the if returning and the call to delete, which would mean your code would still die with an unhandled exception.

    So your code needs to look something like;

    try
    {
      System.IO.File.Delete(myFile)
    }
    catch (System.IO.IOException ioe)
    {
      //Do whatever you want here when the file cannot be deleted, i.e
      MessageBox("Could not delete file because " + ioe.Message);
    }
    • Proposed as answer by Kira Qian Thursday, August 13, 2009 7:33 AM
    • Marked as answer by Kira Qian Tuesday, August 18, 2009 8:03 AM
    Wednesday, August 12, 2009 8:56 PM

All replies

  • You can try to open the file exclusively.  If the file is opened (locked) by another process, you will receive an exception which can be handled.
    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, August 11, 2009 10:35 PM
  • Well, that way i can check it but i was thinking in case if there is any other way of checking it.

    Thanks,
    zolyit
    Tuesday, August 11, 2009 10:42 PM
  • Well, that way i can check it but i was thinking in case if there is any other way of checking it.

    Thanks,
    zolyit

    Post the answer you wanted.
    Tuesday, August 11, 2009 10:50 PM
  • It is not possible in Windows.  In fact, you shouldn't try.  This is inherent in multitasking operating systems.  Say you have a function called IsFileLocked().  It returns False so you open the file.  This will occasionally fail.  Right between the function return and the file open call, Windows pre-empts your thread and another process opens the file.  Notoriously a virus scanner or the Windows Search indexer.  After a while, your thread resumes and make the file open call.  Kaboom.

    The workaround is simple, just open the file.  If you get an IOException, the file was in use.  If you don't, there's no way that any other thread can steal the file away from you.

    Hans Passant.
    • Proposed as answer by Kenneth Haugland Wednesday, August 12, 2009 3:43 AM
    • Marked as answer by Kira Qian Tuesday, August 18, 2009 8:03 AM
    Wednesday, August 12, 2009 1:43 AM
  • I was trying to define one method called  IsFileOpened(filename) that returns bool. And within this file's body, i will attempt to open the file (use try-catch) and if control jumps into catch section then, the method will return true since the file is being used otherwise, will close the file and return false. But, am afraid this can correctly does the job. I think a file can be opened(more than one process simultanously) for reading right? If so, this technique DOESN'T help me any more.  Please help?

    zolyit
    Wednesday, August 12, 2009 6:26 PM
  • Your method IsFileOpen(FileName) will only work if you can open it with read/write access locked.  Then you can return true, because you have it locked and you know it.  The other answers are practical.
    Wednesday, August 12, 2009 6:38 PM
  • Hello JohnWein,

    All over, it wasn't locked when its opened (well, that is how its done), now i am adding a delete operation in my app which generates the error b/c the file might be used by other process. Can you please give me a code snipest or anything that will work for my case.
    Thanks,
    Wednesday, August 12, 2009 7:36 PM
  • As nobugz said you shouldn't test to see if the file is deletable, you should try to delete it and catch the appropriate exception (i.e System.IOException or System.PermissionDeniedException or whatever is thrown in this case) and then handle the error (i.e do whatever you would have done if you had detected the file was in use).

    If you have code like;

    If IsFileLocked(myfile)
    System.IO.File.Delete(myfile);

    The file could be opened in between the if returning and the call to delete, which would mean your code would still die with an unhandled exception.

    So your code needs to look something like;

    try
    {
      System.IO.File.Delete(myFile)
    }
    catch (System.IO.IOException ioe)
    {
      //Do whatever you want here when the file cannot be deleted, i.e
      MessageBox("Could not delete file because " + ioe.Message);
    }
    • Proposed as answer by Kira Qian Thursday, August 13, 2009 7:33 AM
    • Marked as answer by Kira Qian Tuesday, August 18, 2009 8:03 AM
    Wednesday, August 12, 2009 8:56 PM