thewpfguy
thewpfguy

Reputation: 824

How to wait for a file to be copied completely?

I have a thread which polls a folder for new files. The problem is that it sees a new file and starts working on it even before the file has been completely copied by another process. Because of this the poller gets file used by another process error.

Is there a way to check the file is free to use or get notified? We can certainly use exception handling code, but is there a better way?

Tech: .NET 2.0/C#

Update:

Upvotes: 5

Views: 1159

Answers (1)

Jimmie R. Houts
Jimmie R. Houts

Reputation: 7818

We attempt to get a lock on the file before processing it and handle the IOException rather than a generic exception during the attempt to read the file.

See FileStream.Lock on MSDN.

Upvotes: 2

Related Questions