Tutorial :how to check if a file is being accessed by any other processs and release it?



Question:

I am trying to delete/open/edit some files in my C# .Net application.Sometimes i get exception stating the file/directory is being accessed by another process.Is there a way to check if a file/directory is being accessed by process and try to release the file from that process?


Solution:1

First, I suppose there are 2 things that may help you:

  • consider using FileAccess and FileShare flags when opening files
  • if data from the file is needed only withing the scope of the function use the construction

    using(FileStream stream = File.Open(...)) { <file operations> }

    this will ensure that file is closed immediately after exiting 'using' block, and not when FileStream object is collected by GC.

Second, there is an unsafe way to get processes that use the file. It is based on debugging features provided by windows. The main idea is to get all system handles and iterate through them to find which are the files handle and additional information. This is done using functions that I'm not sure are documented. If you are interested use google to find more information, but I do not think it is not a good way.


Solution:2

No. The only way to do this is to try to access the file, and handle the IOException.

Realistically this is the only safe way anyway. Suppose there was a IsFileInUse() method, and you called it, and it returned "nope, nobody's using that file," and you went ahead and accessed the file. The problem is that in the meantime some other process might have locked or deleted the file. So you'd need to put exception handling around your attempt to access the file anyway. The "test by acquiring" model is the only one that is 100% reliable.

If a file is in use by another process, .NET doesn't provide a way of determining which other process that might be. I believe this would require some pretty low-level unmanaged code though I could be wrong. It is a very low-level operation, if it is possible at all, to "release the file from that process" because that would violate the other process' expectations -- e.g. it thinks it is allowed to write to the file but you have deleted the file and garbaged the handle. I believe you would need to terminate the other process if it's not willing to give up its lock voluntarily.


Solution:3

                 public bool IsInUse(string path)                   {                       bool IsFree = true;                      try                      {                          //Just opening the file as open/create                          using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))                          {                          //we can check by using                           fs.CanRead // or                           fs.CanWrite                          }                        }                      catch (IOException ex)                      {                          IsFree = false;                      }                      return IsFree;                   }                            string path = "D:\\test.doc";                       bool IsFileFree = IsInUse(path);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »