Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net FileWatcher fails for ~80+ files

Tags:

c#

.net

I'm using .net 2.0 filewatcher to watch a folder for new files. It works perfectly except when I put more than ~80 files at once. The event just doesn't trigger anymore. It's as if the filewatcher is set to keep track of certain number of files.

For the time being I have asked the user not to put more than 50 files at a time and that seems to work however I would like to fix it so that hundreds of files can be dropped into the folder at once.

Here's the code I'm using for the event. It's pretty standard stuff nothing fancy.


FileWatcher = new FileSystemWatcher();
FileWatcher.Path = ConfigurationManager.AppSettings["FolderOfFilesToWatch"];
FileWatcher.NotifyFilter = NotifyFilters.FileName;
FileWatcher.Filter = "*_*_*.*";
FileWatcher.Created += new FileSystemEventHandler(watcher_Created);
FileWatcher.EnableRaisingEvents = true;


static void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.Write(e.Name);
}

Any ideas?

like image 706
Tigran Avatar asked Jun 02 '09 17:06

Tigran


2 Answers

You probably need to increase the FileSystemWatcher.InternalBufferSize. By default, FileSystemWatcher uses a smaller buffer for performance, and can overflow if too many changes occur in a short time frame.

Try setting a larger buffer size to prevent that from occurring.

like image 75
Reed Copsey Avatar answered Oct 20 '22 05:10

Reed Copsey


I use FileWatcher but I took a "belt and suspenders" approach. Whenever I get a FileWatcher event I stick around and check for files I haven't seen before (in my case I have a file catalog). Believe me, there will be a time when you get thousands of files dropped in a folder and you must have another safeguard to account for all of them.

Another alterative you may look into is change journals, although it is (AFAIK) limited to disks attached to your machine, whereas you can use FileWatcher to watch UNC paths as well.

like image 2
Otávio Décio Avatar answered Oct 20 '22 04:10

Otávio Décio