1

I use FileSystemWatcher to monitor some folder for changes. When it fires file changed event, I need to read that file and update some stuff. The problem is that when this event is fired, target file is still forbidden to read even in this way FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

Is there any other events notifying that file ended change? Or anyway to create some waiter that will wait for file read ability via some standard ways (WinAPI or etc). Or maybe looping try catch is the only choice?

Kosmo零
  • 4,001
  • 9
  • 45
  • 88

2 Answers2

2

You should use the below helper method to return the filestream if it is available.

public static FileStream WaitForFile(string fullName)
{
    FileStream fs = null;
    int numTries = 0;
    while (true)
    {
        ++numTries;
        try
        {
            //try to open the file
            fs = new FileStream(fullName, FileMode.Open, FileAccess.Read, FileShare.None, 100);

            fs.ReadByte();//if it's open, you can read it
            fs.Seek(0, SeekOrigin.Begin);//Since you read one byte, you should move the cursor position to the begining.                    
            break;
        }
        catch (Exception ex)
        {
            if (numTries > 10)//try 10 times
            {
                return fs;//Or you can throw exception
            }
            System.Threading.Thread.Sleep(10000);//wait 
        }
    }
    return fs;
}
Oğuz Sezer
  • 320
  • 3
  • 15
  • horrible and unrealiable work around my friend. Granted you try x amounts and wait for 10 seconds but this really is not efficient nor guaranteed that after x amount of times the file will be unlocked. – Ahmed ilyas Jul 17 '14 at 09:01
  • No it doesn't thats why I return FileStream object as null. That way I can handle if it is readable or not. And If you have a better idea, just provide it. – Oğuz Sezer Jul 17 '14 at 09:03
  • is there any system events available to know when some file closed and available for read? – Kosmo零 Jul 17 '14 at 09:23
  • I'm afraid there isn't any. You can google "FileStream ready" and will come to the solution I offered. "The OnCreated event is raised as soon as a file is created. If a file is being copied or transferred into a watched directory, the OnCreated event will be raised immediately, followed by one or more OnChanged events." says at [MSDN](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.created(v=vs.110).aspx) – Oğuz Sezer Jul 17 '14 at 10:12
  • Well, I'm sure I got what you meant. No there is no way to understand if a file is locked unless you try to access it. So we have to do some work around, and yes, it still doesn't provide a solution if the file is locked more than the trycount * waittime, but at least this works in most cases. – Oğuz Sezer Jul 17 '14 at 12:56
1

Unfornately there's no guarantee that you can access the file when Changed event raised. Here'is a topic whic may interest you in how to wait for the file to be available for Open.

Community
  • 1
  • 1
Perfect28
  • 11,089
  • 3
  • 25
  • 45