How to wait until the file is fully copied?

I have a thread that will poll the folder for new files. The problem is that he sees a new file and begins to work on it even before the file has been completely copied by another process. Because of this, the poller gets the file used by another process error.

Is there a way to check a file that you can use or receive a notification? Of course, we can use exception handling code, but is there a better way?

Technology: .NET 2.0 / C #

Update:

  • Learning from other answers that if we have access to the application that writes the file, then the best design is to start with some other .tmp extension, and then rename it after copying.

  • FileStream.Lock can be used if we do not control the source application

+5
source share
1 answer

We are trying to get a file lock before processing it and to throw an IOException, rather than a general exception, while trying to read the file.

See FileStream.Lock on MSDN.

+2
source

All Articles