FileSystemWatcher on a UNC path throws and Exception when trying to move a file

Posted: 25th August 2010 in General, Windows

After creating a windows service, to move a file from one UNC path to another using a FileSystemWatcher, which, in turn,  monitors a UNC path of an FTP folder for a new file,  I received an unexpected error.

I wired up the FileSystemWatcher’s OnCreated event to monitor for the new file. However, when the event fires and the service tries to move the file to another UNC path an IOException exception is thown with a message “The process cannot access the file because it is being used by another process.”

After some investigation, I found that when the file is dropped into the directory, the event OnCreated event is fired, but, at that point, the file is still writing. As a workaround, I caught the exception and when the file is released, it fires the OnChanged event, where I can successfully move the file.

Not the prettiest solution, but it works!  Anyone with any better solutions, let me know.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s