.Net FileSystemWatcher Class copying large files- events.

mustang_larry used Ask the Experts™
c# .NET file system watcher class.

Using the file system class seems like a nice way to detect
when files appear in a folder.

When copying a large file the created event is fired right away.

What would be the best way to detect that file has been completely copied
before consuming the file for use.

here is a good example of the class.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This question was asked many times, unfortunately, I have never seen satisfactory answer to it. Event is raised before file is copied completely.
The only way I can think about is trying to open this file in a loop. Al time when file is still copied, this attempt throws some kind of Access Denied exception. When file is successfully opened, copying is completed.
Most Valuable Expert 2012
Top Expert 2008

What is the file size when the create event is fired?


When doing FTP, the common practice is to rename the file (with FTPrename) after the transfer is complete.  Your consuming application looks for files with one extension while the transfer is done with a different extension, or, alternatively, you can rename the file between folders.

You may have to modify your file-producing application to rename the file after it is done creating it.

Try this:  

System.Threading.Thread.Sleep(<some value>)

before starting any operation on the file.
another sollution:
I had similar problem, here is how I solved it..Chose time (in miliseconds, to delay action after watched event is fired)
Set code below  under FileSystemWatcher1_Created, just before you want to do copying..

Me.FileSystemWatcher1.WaitForChanged(IO.WatcherChangeTypes.Created, 60000)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial