FileSystemWatcher, determining when the file has been finished copying

  • Thread starter Thread starter Trowa
  • Start date Start date
T

Trowa

Hi,

I'm trying to use FileSystemWatcher to determine when a new file has been
added to a directory. However, after the file has been added, I need to
process the file. This results in a problem if the file is large and takes
time to copy, as the events appear to get received before the copy is
finished.
I've tried listening to the Created event, as well as the Changed event
(which gets fired multiple times) based on the last write time. When
listening to the event, I try checking if I can open the file for exclusive
access to determine if the copy operation is completed, but this still
doesn't seem to work all the time.
Any ideas would be greatly appreciated.
Thanks,



Michael
 
Trowa,

You have to set the notify filter to know when the file copy is completed.

//set the notify filter
myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
;

When the notify filter is set, the event wont be fired multiple times. You
will be notify when the entire operation is completed.

Shak.
 
Trowa, Shakir,

When I use NotifyFilter, the change event still is fired several times
during a copy process. Is there any other way to determine with
FileSystemWatcher if such a file operation is finished ?

Ulf
 
Try this.

myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;

it should work now

Shak.
 
I don't think so!!
The changed event will just fire more than once.
Search from this archive or google, you will find a lot
saying change event will fire more than once.
I just post a question a couple days ago..
Have you really tried that?
Try it, you will see the outcome..
All you can do is use flag and check in a timer_event or comparing trigger time with
last_time_trigger..
 
Ulf Bietz wrote
Trowa, Shakir,

When I use NotifyFilter, the change event still is fired several times
during a copy process. Is there any other way to determine with
FileSystemWatcher if such a file operation is finished ?

We had a similar issue some time ago. Since we controlled the creation of
the file by ourselves (from another application outside the program that had
to watch the directory), we decided to set

myFileSystemWatcher.NotifyFilter = NotifyFilters.FileName;
myFileSystemWatcher.Renamed += RenamedEventHandler(myRenamedEventDelegate);

and rename the file after copying from the original process. The "renamed"
event will be fired only once. But then, we were in control of the copy
process, maybe you are not. But you might try to rename the file on your own
after receiving each "changed" event. I'm not sure, but I think you cannot
rename a file while it is being copied. If finally the rename is successful,
you can process the "renamed" event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top