C# FileSystemWatcher, How to know file copied completely into the watch folder

It is indeed a bummer that FileSystemWatcher (and the underlying ReadDirectoryChangesW API) provide no way to get notified when a new file has been fully created.

The best and safest way around this that I've come across so far (and that doesn't rely on timers) goes like this:

Upon receiving the Created event, start a thread that, in a loop, checks whether the file is still locked (using an appropriate retry interval and maximum retry count). The only way to check if a file is locked is by trying to open it with exclusive access: If it succeeds (not throwing an IOException), then the File is done copying, and your thread can raise an appropriate event (e.g. FileCopyCompleted).


I have had the exact same problem, and solved it this way:

  1. Set FileSystemWatcher to notify when files are created and when they are modified.
  2. When a notification comes in:

    a. If there is no timer set for this filename (see below), set a timer to expire in a suitable interval (I commonly use 1 second).

    b. If there is a timer set for this filename, cancel the timer and set a new one to expire in the same interval.

When a timer expires, you know that the associated file has been created or modified and has been untouched for the time interval. This means that the copy/modify is probably done and you can now process it.