FileSystemWatcher OnCreated only shooting for the first file with multiple copied files

I have a FileSystemWatcher that I would like to run an OnCreated event for each folder copied to the directory Iโ€™ve viewed . Several folders will be copied to this browsing directory manually again.

Currently, it fires only event for the copied first folder.
Therefore, if I look at folder X and select folders A, B, C in Windows Explorer and copy them to X, OnCreated starts for A, but not B or C.

This is my code that I use to configure 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; 

and here is my OnCeated method

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

Any idea why this only fires the event for the first folder copied to the watched directory?

+4
source share
1 answer

From the documentation :

The Windows operating system notifies your component of file changes in the buffer created by the FileSystemWatcher file system. If many changes occur in a short time, the buffer may overflow. This causes the component to lose track of changes in the directory, and it will provide only a hidden notification . Increasing the buffer size using the InternalBufferSize property is expensive because it comes from non-bootable memory that cannot be swapped to disk, so be so small that they do not reduce any file change events. To avoid buffer overflows, use the NotifyFilter and IncludeSubdirectories properties so you can filter out notifications of unwanted changes.

This seems to be an internal limitation.

I believe that inserting all three folders at once is considered "a large number of changes in a short time" - can you use NotifyFilter and ignore some events?

+10
source

Source: https://habr.com/ru/post/1434912/


All Articles