FileSystemWatcher

  • Thread starter Thread starter Kyle Jedrusiak
  • Start date Start date
K

Kyle Jedrusiak

I'm trying to make us of the FileSystemWatcher.

I have it watching a folder for creations and deletions of files.

When files are added (by an outside process) to this folder I do get the
event for the creation.

The problem is, I need an event after the file has been completely created
and closed, not the second the file is created.
(It make a number of seconds for the complete writing of the contents of the
file to complete.)

Would someone please point me in the right direction?
 
I queue up the files as they are created and check each one every minute.
Once the file is non-zero in size and the same size for two consecutive
checks, I assume it's been completely written to. Note, this may not work
in your case but it's a possible solution. They underlying Windows API
doesn't provide the hook you're looking for.

Mike.
 
You can do two easy things:
- take a delay of n seconds before opening the file, where n is > max time
to write and close the file.
- Try opening the file in exclusive and if that fails, wait a bit and retry
until it succeeds.

The better way is, if you can control the file creation process, to listen
only file rename notifications.
After the programs write the file, the last thing they do is rename the file
what you catch. The renaming is fast and when you get the notification, it's
readable.
 
Back
Top