How to test if a file is currently being written to

后端 未结 9 1762
野性不改
野性不改 2020-12-18 21:42

I have an application that must check a folder and read any files that are copied into it. How do I test if a file in that folder is currently being written to? I only want

9条回答
  •  难免孤独
    2020-12-18 22:23

    Catching exception is expensive you should try to use this:

    http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

    There's a super clear example on the msdn page. That'll give you nice events on change, created, delete, renamed.

    copy the file into the folder and rename when your done catch the rename event and your sure your not reading files that aren't finished yet.

    Or you could catch the change events until (this is tricky) they aren't fired anymore.

提交回复
热议问题