Reliable way of monitoring file changes in a directory using the .NET framework

自作多情 提交于 2019-12-09 13:11:39

问题


I'm looking for a reliable way of looking for changes in a directory.

I have tried using the FileSystemWatcher, but it's rather inaccurate when many small files are created, changed or deleted. It misses about 1 or 2 % of the files in my tests. That is quite a lot when you are adding or changing thousands of files rapidly.

I have tried polling for changes at different intervals 500 ms, 2000 ms etc. In this case I get too many hits. That might have something to do with the resolution of timestamps on the FileInfo object.

So my question is; is it possible, using the .NET Framework, to get the the changes to a directory reliably?

-- Christian


回答1:


Have you tried increasing the InternalBufferSize? What size have you set it to?

From MSDN:


Note that a FileSystemWatcher may miss an event when the buffer size is exceeded. To avoid missing events, follow these guidelines: Increase the buffer size by setting the InternalBufferSize property. Avoid watching files with long file names, because a long file name contributes to filling up the buffer. Consider renaming these files using shorter names.


Keep your event handling code as short as possible.



来源:https://stackoverflow.com/questions/4855646/reliable-way-of-monitoring-file-changes-in-a-directory-using-the-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!