views:

280

answers:

4

Hey guys / gals,

I have come across several "solutions" here and across the web but none seem to fit the bill.

What I am looking to do is have an app monitor a folder for new files (either by creation, a move, or a copy) and perform actions on those objects. That being the scenario, I turned to the FileSystemWatcher class to perform this action.

The problem is that the file FileSystemWatcher.Created event is fired before the entire file is created (most noticeably seen through a copy of a large file).

Is there any way to have this event fire at the conclusion of the file creation as opposed to the beginning? I have tried various combination's of the FileSystemWatcher.NofityFilter property with no success.

Thanks in advance! :)

A: 

Hmm interesting problem. I never used the object while watching for big files. Did a little searching and seems one solution is to monitor the Changed event as well. Because once the file is done copying (after created is fired) a changed event is thrown as well (cause the file increased in size)

More details from what I read here: http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/f84bb7c8-b7d5-44da-b0f3-6d1a70415d11/

townsean
but I unfortunately had no luck finding a more elegant solution to this :( (at least yet, I'd really like to know)
townsean
+1  A: 

I have used a couple of solutions for this situation.

  1. If you can work with the creator of the file and use a renaming scheme for the file. EG. Create the File as _Name while being created and at the end of the process rename it to Name and the event will fire and you have a complete file.

  2. When your trigger fires check if you can get an exclusive readonly lock on the file. If you can then the write operation has been completed to the file. (I wrote something about this in another question http://stackoverflow.com/questions/3167647/keep-settings-in-sync-between-forms-application-and-windows-service-or-any-n-tie/3278034#3278034)

You could possibly integrate something like #2 into your Changed Event and then you'll get the result.

Paul Farry
A: 

You would need to track closing of the file after it's creation and I doubt it's possible with FileSystemWatcher. If you don't find a solution with FileSystemWatcher, take a look at CallbackFilter, which lets you track all operations in real-time.

Eugene Mayevski 'EldoS Corp
A: 

Hi, I know, that what I am going to tell you does not look elegant. I had also to monitor files that arrive from different places, some of them were large and some small. We found out, that FileSystemWatcher is not reliable for this purpose. If you want to be 100% sure, you can check once in a while, using Timer class and its Elapsed event.

Boris Modylevsky
Hey Boris, in what aspects was it not reliable?
nokturnal
We used FileSystemWatcher for monitoring generation of files by some other system. Files had to be generated at specific times with predefined names. In case file is not generated within its time slot, monitor sent alert. Many times FileSystemWatcher "missed" or modifications of files which led to many "false" alerts. I searched on the web, and found that the problem is common to others as well.
Boris Modylevsky