error in "OnCreated" event of FileSystemWatcher class

G

Guest

Hi,
I am coding a Windows Service in which I am using the "OnCreated" event of
FileSystemWatcher class to do some processing when a new file is created in a
particular folder. However I have encountered the following issue in the
"OnCreated" event of FileSystemWatcher class.

Whenever a file is copied from a different folder (like network share) and
pasted to the monitoring folder and this copy & paste process takes time,
even before the file is fully pasted to the monitoring folder, the OnCreated
event of the FileSystemWatcher Class is treating the file as a new file. So
the code written to access the file to read the contents throws the below
error message
“The process cannot access the file because it is being used by another
process.“

Please let me know why the OnCreated event is recognizing a file as new even
before it has been fully pasted the folder. Also please let me know the best
recommended solution to this issue.

Regards,
Mahita
 
G

Guest

That is simply because the file is first created, then the contents of
the file is written to it.

The notification is created when the file has been created, but before
the contents is copied. For small files, the time from when the
notification is created until the event is handled in your program is
usually enough to copy the contents.

The issue can occur for any file that is copied, not only large files,
but it's more likely to occur the larger the file is.
 

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

Top