开发者

How to wait for a file to be copied completely?

开发者 https://www.devze.com 2023-01-14 21:22 出处:网络
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

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:

  • Found out from other answers that if we have access to the app writing the file then better design is to start with some other extension .tmp and then rename it after copying.

  • 开发者_StackOverflow中文版
  • The FileStream.Lock could be used if we don't control the source application


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.

0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消