Copying a file after the "Created" event with FileSystemWatcher

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Given a scenario where I'm monitoring a folder, once the file has been
created, I'd like to have it relocated to another folder via the
File.Copy() method.

My problem occurs when I paste a 600mb+ file into the folder I'm
monitoring. I use this magnitude of a size in order to guarantee it'll
work for smaller files.

The "created" event occurs before the file is actually completely
copied into the folder, so when I try to do File.Copy(), nothing
happens.

Is there anyway to set a NotifyFilter to respond ONLY when the file
has been "completely" created?
 
Is there anyway to set a NotifyFilter to respond ONLY when the file
has been "completely" created?

Not to my knowledge. I have used two methods. The first method is to have
the file placed in the folder using a temporary name and then rename it to
the expected name. Then handle the Rename event of the FileSystemWatcher.

The second is when the Create event occurs, attempt to open the file
exclusively. If that fails, then the file is still being copied in. Keep
attempting to open the file in a loop until you are successful. Then you
can perform the file.copy.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top