1

To avoid exceptions like

(1) Process cannot access the file because it was used by another process

I used the following method to test the accessibility of the file before any further processing.

private bool CheckIfFileBeingUsed(string FilePath)
    {
        FileStream Fs = null;
        try
        {
            Fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
            Fs.Close();
        }
        catch (Exception)
        {
            return true; //Error File is being used
        }
        return false; //File is not being used.
    }

Could anyone advise me there's any Windows API or other solutions for such testing of file accessibility instead of the above File.Open Method ?

Derek
  • 2,185
  • 4
  • 20
  • 24
  • Related: http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c – Daniel Sep 01 '11 at 07:29
  • 1
    Terminology confusion: you want to check if the file is locked, but normally "accessibility" would refer to whether you have access/permissions to use the file. – Richard Sep 01 '11 at 08:07
  • Dear sir, would getting the properties of the file resulting in accessing / reading the file ? and that would that result in an exception if the file is not ready ? – Derek Sep 01 '11 at 08:08
  • What would you do if the file was locked by some other process between this check passing and opening for the real access? This approach is fundamentally flawed: just open for real and deal with the failure there (including looping for a retry with a delay if that's what you need). – Richard Sep 01 '11 at 08:10

4 Answers4

6

It seems like you're duplicating effort here. Since trying to open the file for processing will throw the same exception you're catching here, why not just proceed with an attempt at processing the file, and handle the exception if it comes up?

Assuming that's not possible for some reason, testing like this still isn't totally reliable, since you now have a race condition. Even if this test passes, there's no guarantee that the file will be in the same state when you go to run your other code on the file. That's why it's easier to just attempt to do what you want, and handle any exceptions at that point.

dlev
  • 48,024
  • 5
  • 125
  • 132
  • My processing would be trying to get the properties of the file using the FileInfo class but it will results in the above exception. Thats why I was looking out for checks to be in placed before getting the file properties. – Derek Sep 01 '11 at 07:29
  • 1
    @Derek I still think the point stands; if you get an exception from inspecting the file, then handle that exception, rather than trying to test for a new one. Is there a reason that doesn't work for you? – dlev Sep 01 '11 at 07:32
  • Dear sir, would getting the properties of the file resulting in accessing / reading the file ? – Derek Sep 01 '11 at 07:46
  • @Derek I'm not sure; I had thought from your question that *you* knew the answer! If it doesn't, then why would you care if the file was otherwise in use? – dlev Sep 01 '11 at 07:48
1

What is the purpose of this check in the first place? You are trying to avoid exception by making it occur earlier and catching it, but why not catch it with your current design? Also, this approach is subject to race condition because file might become non accessible just after your check, so the only correct way to do it is to just do any processing you need and catch exceptions thrown.

Konstantin Oznobihin
  • 5,234
  • 24
  • 31
  • My processing design would be trying to get the properties of the file using the FileInfo class but it will results in the above exception. Thats why I was looking out for checks to be in placed before getting the file properties. – Derek Sep 01 '11 at 07:49
  • 1
    So you can catch the exception in the CheckIfFileBeingUsed, why can't you do it when you are using FileInfo? Anyhow, again, the check is just useless and will not solve you issue. The only way to do it, is to handle this exception when you are reading file properties. – Konstantin Oznobihin Sep 01 '11 at 07:53
  • Dear sir, would getting the properties of the file resulting in accessing / reading the file ? and that would that result in an exception if the file is not ready ? – Derek Sep 01 '11 at 07:59
  • I think, you should know better. Anyhow, if there would be no exception, then you have no issues, otherwise just handle the exception. What problem do you have with handling it? – Konstantin Oznobihin Sep 01 '11 at 08:09
0

Your method seem valid to me. Don't forget to close the stream in case of an exception though:

    finally
    {
        if (Fs!= null)
            Fs.Close();
    }

Keep in mind that there is no 100% guarantee that this will prevent the 'file in use' exception. There is a always a very brief (ms) period between your check and actually opening the file, in which the file could become in use by another process.

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
0

As far as you handle the file exists check explicitly, this method is fine. Your method returns true even if File Doesn't Exist

I don't think C# provides any API for this.

sri
  • 1,005
  • 1
  • 12
  • 26