Check if a file is open

63,464

Solution 1

protected virtual bool IsFileinUse(FileInfo file)
{
     FileStream stream = null;

     try
     {
         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
     }
     catch (IOException)
     {
         //the file is unavailable because it is:
         //still being written to
         //or being processed by another thread
         //or does not exist (has already been processed)
         return true;
     }
     finally
     {
         if (stream != null)
         stream.Close();
     }
     return false; 
}

Solution 2

As @pranay rana, but we need to make sure we close our file handle:

public bool IsFileInUse(string path)
{
  if (string.IsNullOrEmpty(path))
    throw new ArgumentException("'path' cannot be null or empty.", "path");

  try {
    using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { }
  } catch (IOException) {
    return true;
  }

  return false;
}

Solution 3

If you mean that you want to check if a file is open before you try to open it, then no. (At least not without going low level and examine every file handle that is open in the system.)

Besides, the information would be old when you get it. Even if the test would return that the file is not open, it could have been opened before you have a chance to use the return value.

So, the proper way to handle the situation is to try to open the file, and handle any error than may occur.

Share:
63,464
Md. Rashim Uddin
Author by

Md. Rashim Uddin

I am nothing more than a simple man with the high vision of evolving multifaceted software systems by using my technical and interpersonal skills which will complement my professional growth. I feel enjoyment to take challenges to resolve those technical problems which is yet to be solved.

Updated on November 22, 2020

Comments

  • Md. Rashim Uddin
    Md. Rashim Uddin over 3 years

    Is there a way to find if a file is already open or not?

  • hemp
    hemp almost 14 years
    If the process doesn't have write access to the file this will produce a misleading result (the open will fail, but not necessarily because a handle exists.) Opening for Read access would be less error-prone.
  • hookenz
    hookenz almost 14 years
    Amazing that most languages don't have a method to test file is open. We used to use the same type of method under C++ in OS/2. i.e. try to open the file exclusive. It works well enough, but I've never thought it elegant.
  • hemp
    hemp almost 14 years
    It's not a language feature, it's an OS feature. There just doesn't exist a simple API (in Windows) to query for that information. It is possible to get, but it's low-level and there are a lot of parameters that would have to be specified to know what was meant by "open".
  • Nate Sauber
    Nate Sauber over 11 years
    This answer is identical to this answer on another question: (stackoverflow.com/a/937558/38657)... that said, it's a good answer.
  • James
    James over 7 years
    I just tested this and an exception is not thrown when the text file is open in Notepad.
  • James
    James over 7 years
    Same as @pranay rana, I tested this method, but it does not detect when the file is open in Notepad.
  • Mehdi Dehghani
    Mehdi Dehghani about 7 years
    -1 because maybe after closing the file in you method (IsFileinUse) maybe another thread/process lock the file before your thread