I have written a polling service that checks a directory for new files & then distributes them to new locations.

The service is so quick, that no soon as the file has arrived in the watched folder, it is moved in to its new location.

One concern that I have is that the source files may take 10-15 seconds to be fully written to due to the amount of data being written out. I need to put a safe guard in place that checks that the file is not currently being written to before it gets moved.

Currently, my code simply does a quick naming convention check before executing the move code.


I need to put inbetween this code and the code that moves the file a check to ensure the file is 'finished with' by the system that is writting it out.

Can anyone offer me any assistance on this?

Many thanks

You could try to open the file with exclusive access:

Filestream fs = File.Open(myFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

As long as another process is using the file this call will fail.

ok cool - that seems to do the job!

Cheers, I have built it to reloop on a caught exception and it seems to handle that nicely enough. i might put a count on that to avoid getting stuck in an infinative loop but other than that im sorted.

This question has already been answered. Start a new discussion instead.