FileSystemWatcher[?]

Hi,

I need some help regarding filesystemwatcher class.
The main issue concerned here is, when a folder is being monitored and a bulk amount of files is added/copied to that folder, I need to know when the file copy is over. I can take action one that mass of files copied later on.

if "c:\myfolder" is the file monitored and if i dump 10,000 files into it, certain event are raised by the file watcher class. that's fine. But i need to know the end point of bulk copy over.

Any suitable suggestions are appreciated along with code snippet.


Thanks in advance.


Answer this question

FileSystemWatcher[?]

  • Yousuf Khan

    Rather than attempting to rename it, you can just attempt to open it in exclusive mode.

  • Sid007

    That's the way I've done things too.

    An alternative approach is to create a special "semaphore file" with a special suffix at the end of the process. Then you can watch for a file appearing with that suffix, and act when it shows up.

  • hollander67

    FWIW, I've been playing around a little with FileSystemWatcher. It's useful, but its inability to detect when a file write operation is complete is a definite drawback. A potential work around that relies on a file rename operation does seem to work. That is, filter events on:

    water.NotifyFilter = NotifyFilters.LastWrite

    When this event fires, attempt to rename the file within a try-catch block. If the rename fails, do nothing, if it succeeds, you are reasonably sure that the file write has completed. However, I found that I needed to add a short delay (500msec ) before attempting the rename - otherwise the rename would still fail occasionally, even when the file write was done.

    I don't have control over the processes that are writing files into the directory I want to monitor so this is the only way I found to implement this. Is there a simple, reliable way to test a file to see if it has any write locks on it from other processes FileInfo.MoveTo() seems to be the only thing I've found. Has anyone been able to build a reliable system based on FileSystemWatcher



  • Tommmy77

    From memory the fle system watcher wont be able to help here as it generally only used for the general file system events, ie renamed, deleted, and such.

    For the problem that you have I have done something simular (Two different ways). The first was to have the bulk process transfer the files with an extension of pro (Processing), then once the transfer was complete the tranfer process would rename the files with the ext of cpt (Complete). Once done the file wather would wait for a rename event then do it's thing. The secode was for the transfer process to send an emtpy file with the same file name and the extenstion of .ok, when the file watcher new about the .ok file being created it new that it could start processing the transfered file....

    Hope this helps.

    Mykre
    www.ircomm.net - Managed DirectX Game Programming

  • Kishanb

    Hi,I am having almost same problem as to how to determind that a transfer was complete I have a folder and I have a file watcher application where I ahve an event called FileAdded which is raise when a file is added to that folder. I handel that event in a eventhandler called OnFileAdded. But my problem is how to find out that a file transfer to that folder is complete If you can please help me with code snippet. Thank you in advance.Bharat.

  • FileSystemWatcher[?]