使用.NET Framework监视目录中文件更改的可靠方法 [英] Reliable way of monitoring file changes in a directory using the .NET framework

查看:170
本文介绍了使用.NET Framework监视目录中文件更改的可靠方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可靠的方法来查找目录中的更改.

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

我曾尝试使用FileSystemWatcher,但是当创建,更改或删除许多小文件时,这相当不准确.在我的测试中,它丢失了大约1%或2%的文件.快速添加或更改成千上万个文件时,这是很多事情.

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.

我尝试以500 ms,2000 ms等不同的间隔轮询更改.在这种情况下,我的点击次数过多.这可能与FileInfo对象上时间戳的解析有关.

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.

所以我的问题是;是否可以使用.NET Framework可靠地将更改保存到目录中?

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

- 基督徒

推荐答案

您是否尝试过增加

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

来自 MSDN :

请注意,FileSystemWatcher可能会丢失 缓冲区大小为时的事件 超过.为了避免遗漏事件, 请遵循以下准则:增加 通过设置缓冲区大小 InternalBufferSize属性.避免 观看带有长文件名的文件, 因为文件名很长 填满缓冲区.考虑 使用更短的名称重命名这些文件 名称.

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.

这篇关于使用.NET Framework监视目录中文件更改的可靠方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆