23

Possible Duplicate:
Wait until file is unlocked in .NET

I have an open file, like a .Doc or .txt, and I have to wait until the user close it. I already try this, according to Wait until file is unlocked in .NET :

while (true)
{
    try
    {
      using (FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 100))
        {
            //the file is close
            break;
        }
    }
    catch (IOException)
    {
        //wait and retry
        Thread.Sleep(1000);
    }
}

This works well ,but it may be possible to find a solution without a try/catch and handler the exception ?

Community
  • 1
  • 1
Guillaume V
  • 931
  • 2
  • 10
  • 27
  • 4
    This is a very good question. – TIHan Sep 09 '11 at 19:06
  • Unfortunately, you can't, in general. – SLaks Sep 09 '11 at 19:07
  • 4
    There's already a question for this - you even posted a link - so if that's the answer, that's the answer. It's really too bad there's not a clean way, though. – Ry- Sep 09 '11 at 19:07
  • Note that you have 2 questions in one - "when user is done with the file" and "when file is no longer locked". The second portion is exact duplicate of the other question, first one is not solvable for all file type/application pairs (i.e. if application like Visual Studion opens a file and do not keep a lock on it but let user save whenever needed). – Alexei Levenkov Sep 09 '11 at 19:37
  • I'm sorry for the duplicate, but thank for the comment and the answers. Now i understand much more the concept. – Guillaume V Sep 09 '11 at 20:23

3 Answers3

10

Unfortunately no, there is no other way.

The API doesn't have an event that will fire when a file in unlocked or anything else that is convenient.

Retrying with waits is the best solution with the current API.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Even though you may be absolutely right, I still wonder how Mark Russinovich's Process Explorer manages to display process' open handles almost in real time. Does it also do it by frequent polling? I will try to figure this out, and if I find something useful I'll post my answer here. – Igor Korkhov Sep 09 '11 at 19:35
  • @Igor - He isn't using managed code. I don't know what the windows API exposes (and he might be using _undocumented_ features). – Oded Sep 09 '11 at 19:37
  • 2
    Sure Mark is using low-level unmanaged API (or even dynamically loads kernel driver!), but the OP dind't say that the problem must be solved by using managed API only. It is really interesting for me (and for many others as well, I bet) how to do it without wait-and-loop approach. – Igor Korkhov Sep 09 '11 at 19:53
  • You can use the Restart Manager API to find which processes are locking a specific file: https://msdn.microsoft.com/en-us/library/windows/desktop/cc948910(v=vs.85).aspx But it does not allow to monitoring a file locking state I think. – Maxence Aug 10 '15 at 09:31
2

For one, though, don't use the loop you have right now, breaking if there's no exception - perform your actual file access in that using loop.

Next, if the file is open in a known process, you could get its Process object, set EnableRaisingEvents to true, and handle its Exited event to try again. It's not failsafe, though, so you would still handle exceptions and use a loop.

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

You can make P/Invoke calls to native CreateFile function and then analyze error code. However, try/catch will still be necessary.

Vadym Stetsiak
  • 1,974
  • 18
  • 22