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

后端 未结 9 1269
小鲜肉
小鲜肉 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:46

    Not sure about "the correct way", but you could use the monitoring tool (FileSystemWatcher I guess) to fill an internal queue that you use for delayed processing. Or better yet: just use a queue to place files in that had the open fail, so you can retry them later.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2021-01-17 12:54

    one approach that i take always is to create a file in the end of my copy/transfer named "token.txt" without content. The idea is that this file will be created just in the end of the transfer operation, so you can monitor this file creation and when this file is created, you start to work with your files. Don't forget to erase this token file always when you start to process your files.

    0 讨论(0)
提交回复
热议问题