tags:

views:

30

answers:

1

I am using a FileSystemWatcher on a directory and added its event handlers, set its EnableRaisingEvents to true and IncludeSubdirectories to false and added NotifyFilters.

While running the application if I create new folders in the specified directory sometime I get

FileNotFoundException : "An error occurred while reading a directory".

The StackTrace is:

System.IO.FileSystemWatcher.StartRaisingEvents() System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)

What can be root cause of the problem?

Add: Can anybody throw some light on what this StartRaisingEvents() dos?

+1  A: 

This is typically because the FileSystemWatcher can be unreliable. The folder may not "fully" exist when you get the events. You may need to retry with sufficient pauses and do various Directory.Exists() checks before actually performing IO operations.

Neil Barnwell
I've had to put the thread to sleep to ensure that file has been successfully created before performing operations on the file.
dhirschl