C#: Monitoring copied or moved files with FileSystemWatcher

后端 未结 4 1306
醉梦人生
醉梦人生 2021-02-02 03:17

I have come across several \"solutions\" here and across the web but none seem to fit the bill.

What I am looking to do is have an app monitor a folder for new files (ei

相关标签:
4条回答
  • 2021-02-02 03:35

    I have used a couple of solutions for this situation.

    1. If you can work with the creator of the file and use a renaming scheme for the file. EG. Create the File as __Name_ while being created and at the end of the process rename it to Name and the event will fire and you have a complete file.

    2. When your trigger fires check if you can get an exclusive readonly lock on the file. If you can then the write operation has been completed to the file. (I wrote something about this in another question Keep settings in sync between forms application and windows service (or any n-tier, really))

    You could possibly integrate something like #2 into your Changed Event and then you'll get the result.

    0 讨论(0)
  • 2021-02-02 03:45

    Hmm interesting problem. I never used the object while watching for big files. Did a little searching and seems one solution is to monitor the Changed event as well. Because once the file is done copying (after created is fired) a changed event is thrown as well (cause the file increased in size)

    More details from what I read here: http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/f84bb7c8-b7d5-44da-b0f3-6d1a70415d11/

    0 讨论(0)
  • 2021-02-02 03:50

    I know, that what I am going to tell you does not look elegant. I had also to monitor files that arrive from different places, some of them were large and some small. We found out, that FileSystemWatcher is not reliable for this purpose. If you want to be 100% sure, you can check once in a while, using Timer class and its Elapsed event.

    0 讨论(0)
  • 2021-02-02 03:50

    You would need to track closing of the file after it's creation and I doubt it's possible with FileSystemWatcher. If you don't find a solution with FileSystemWatcher, take a look at our CallbackFilter product, which lets you track all operations in real-time.

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