How to test if a file is fully copied in .NET

后端 未结 9 1378
小鲜肉
小鲜肉 2021-01-17 12:27

I am monitoring a folder for new files and need to process them. The problem is that occasionally file opening fails, because system has not finished copying it.

Wha

9条回答
  •  一个人的身影
    2021-01-17 12:49

    It depends, a retry loop is probably the best you can do, if you have no control over the copy process.

    If you do have control:

    • If the folder is local, you could require that the people writing stuff into it lock the file for exclusive access, and only release the lock when they are done (which I think is default for File.Copy). On the .Net side you could have a simple retry loop, with a cool down period.
      • Alternatively you can write the file to a temp folder and only after written move it to the target dir. This reduces the window where bad stuff can happen (but does not eliminate it)
    • If the folder is an SMB share, there is a chance LockFile does not even work (some linux implementations). In that case the common approach is to have a sort of lock file, that is deleted once the person that creates the file is done. The problem with a lock file approach is that if you forget to delete it you can be in trouble.
    • In wake of these complications I would recommend that receiving the data via a WCF service or a web service may be advantageous cause you have much better control.

提交回复
热议问题