I have tackled this exact issue, and there is no built-in way to detect
when the file is free of locks by other processes. Who knows, some
user may have a Word document open and locked in that folder, and your
code needs to be able to handle this situation. Ok, I know that
probably won't happen, but another process has the file locked if it's
still being written. My solution was to listen for FileName and
LastWrite events. I would then attempt to access the file. If it is
locked, you'll just have a try again later. Ahh, but you won't get
another FileSystemWatcher event for that file, so you'll just have to
be innovative. Yes, you can poll and swallow the exceptions that occur
if the file is locked, but be careful that your program doesn't hang if
the file doesn't become unlocked (or if the file is very large).
If only a few files come through, you may want to sent a delegate to
the threadpool to sleep for a second and try to process the file again.
.. . and not give up until it is successful. This way, your main
program can continue to function.
The bottom line is that you will have to be innovative about how you
solve this problem because the solution will have to be unique to your
scenario.
Best regards,
Jeffrey Palermo
Blog:
http://www.jeffreypalermo.com