20

This is how I do it at the moment. I try to open the file with the FileShare set to none. So I want exclusive accesss to the file. If I can't get that then its a good bet somebody else has the file locked.

There's got to be a better and faster way. Any ideas?

            try
            {
                using (FileStream fs = File.Open(GetLockFilename(), FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    fs.Close();
                }
                // The file is not locked
            }
            catch (Exception)
            {
                // The file is locked
            }
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32

4 Answers4

26

There is no need first to check if the file is locked and then access it, as between the check and the access some other process may still get a lock on the file. So, what you do is correct, if you succeed, do your work with the file.

Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
5

The truth is, even if you do figure out a way to check if the file is "locked" by the time you get to the next line where you open the file, something else in the OS may try to get a hold of that file, and your code to open it will fail anyway. You'll have to put a try/catch there anyway. Therefore, I say no. There isn't really a better solution.

BFree
  • 102,548
  • 21
  • 159
  • 201
1

To answer your question, it would be more efficient to write the following extension method for the FileInfo class:

      public static bool IsLocked(this FileInfo f)
    {
            try 
            {
                string fpath = f.FullName;
                FileStream fs = File.OpenWrite(fpath);
                fs.Close();        
                return false;
            }

            catch (Exception) { return true; }      
    }

Once you have created the method, you can do a quick check like this:

  FileInfo fi = new FileInfo(@"C:\4067918.TIF");
  if (!fi.IsLocked()) { //DO SOMETHING HERE; }
  • 3
    This doesn't prevent the race condition. The file could still get locked immediately after this method returns false. – Brian Rasmussen May 12 '15 at 15:32
  • Yes, agreed. The method is not fool proof but the chance of the file being locked between the time the method executes and the time the next line of code runs would likely be slim depending on the application at hand. It is better than not checking at all. If the method returns true, you can write logic to handle that condition (e.g. wait and retry 3 times, etc) before handling with a more general exception. – Marcus Santodonato May 12 '15 at 15:39
  • This code is problematic because if fpath points to a path that does not exist, it will create a 0KB file. – t98907 May 09 '23 at 03:14
1

No that I am aware of, there is no call to check if the file is in use - you have to try to open it and handle the exception as you are doing. Another problem is that it is hard to distinguish between in use and no access allowed.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228