FileSystemWatcher的OnCreated只发射了多个文件复制第一个文件文件、第一个、多个、射了

2023-09-03 05:14:38 作者:oツ

我有一个 FileSystemWatcher的,我想触发一个 OnCreated事件 为每文件夹复制到监控目录的。几个文件夹将手动复制到该目录中看到一次。

I have a FileSystemWatcher that I would like to fire an OnCreated event for every folder copied into the watched directory. Several folders will be copied into this watched directory at once, manually.

目前它只是发射了事件用于复制的第一个文件夹。 所以,如果我在看文件夹X和选择文件夹A,B,C在Windows资源管理器,并将它们复制到X, OnCreated 被激发了A,但不是B或C

Currently it is only firing the event for the first folder copied. So if I'm watching folder X and select folders A,B,C in windows explorer and copy them into X, OnCreated is fired for A but not B or C.

这是我使用的设置我的code中的 FileSystemWatcher的

This is my code that I'm using to set up the FileSystemWatcher:

watcher = new System.IO.FileSystemWatcher(watchPath);            
watcher.InternalBufferSize = 32768;
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | 
                       NotifyFilters.CreationTime | NotifyFilters.LastWrite;


watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;

和这里是我的 OnCeated 方法

void OnCeated(object sender, FileSystemEventArgs e)
{            
    XDocument xmlDoc = BeginImport(e.FullPath); 
}

任何想法的这是为什么只有触发事件的第一个文件夹复制到监控目录?

Any idea why this is only firing the event for the first folder copied into the watched directory?

推荐答案

从文档:

Windows操作系统会通知您的文件更改组件由FileSystemWatcher的创建一个缓冲区。 如果有很多的变化在很短的时间内,缓冲区可能会溢出。这会导致失去跟踪变化目录的组成部分,它只会提供毛毯通知。增加与InternalBufferSize属性缓冲区的大小是昂贵的,因为它来自无法换出到磁盘的非分页内存,所以保持缓冲区小但足够大,不会错过任何文件更改事件。 为了避免缓冲区溢出,使用NotifyFilter和IncludeSubdirectories属性,这样就可以过滤掉不需要的更改通知。

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

这似乎是一个内部的限制。

It seems to be an internal limitation.

我相信粘贴三个文件夹一次的行为被认为是许多变化在很短的时间 - 你可以使用NotifyFilter并留下了一些事件?

I believe the act of pasting all three folder at once is considered "many changes in a short time" -- can you use the NotifyFilter and leave out some events?

 
精彩推荐
图片推荐