I'm trying to use FileSystemWatcher

  • Thread starter Thread starter somequestion
  • Start date Start date
S

somequestion

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,
 
Hi,

Have you tried setting the EnableRaisingEvents property of the
FileSystemWatcher to false when you start processing and then set it back to
true when you're done?

Joe
 
of course, i used EnableRaisingEvents property ...
but it is nothing to do with this property
if i can know about file copy process is finished ?
after that i just invoke Create Event..it will work ...
how can i know the way about checking file copy has finished...
 
Hi
i had the same problem

So i create a file queue (a simple ilist) and a timer
on a file created event i try to process the file (for example for moving it
into another directory)
if the process fail i add the file reference in queue

every 60secs (for example) for each file in the queue i create a thread who
process the file
if the process is complete i delete file from queue
else this file will be reprocessed at the next tick

Thierry
 
I fixed the same thing in my program by getting file info, waiting a
specififed amount of time (I used 3-tenths of a second) then checking
fileinfo.length again.

PSeudoCode :

Do
{
Size1 = FileInfo(myFile).length;
Thread.Sleep(300);
Size2 = FileInfo(myFile).length;
} while (Size1 != Size2);


This simply waits until the file stops growing before processing. It is
notably a hack, but it works like a dream in my system.
 
Back
Top