سؤال

I've got a class that watches for new folder being created. The code works well when I copy and past folders manually, but when a folder is replicated via Distributed File system Replication, the filesystemwatcher doesnt detect the new folder... Any ways to fix this?

I'm running on windows server 2008, the windows service is running in .net 4.5.

My plan B is polling the folder every second with a Task. But I'd rather stick with the filesystemwatcher over reinventing the wheel.

profileFolderMonitor = new FileSystemWatcher(path);
profileFolderMonitor.NotifyFilter = NotifyFilters.DirectoryName;
profileFolderMonitor.Error += (sender, args) => Monitor();
profileFolderMonitor.Created += OnFolderCreated;
profileFolderMonitor.EnableRaisingEvents = true;
هل كانت مفيدة؟

المحلول

I opted for having a simple class doing that keeps state of the currently registered files/foders, has a loop that verifies for any deltas (with a 1 second pause), and updates the state when deltas do occur. I submit FileSystemEventArgs thru event handlers just like the Microsoft FileSystemWatcher this way it integrates well with my code base.

نصائح أخرى

I have continued to have issues with FileSystemWatcher for years. I ended up making something that merged the FileSystemWatcher with a polling option, so that events the FileSystemWatcher wouldn't pick up for some reason wouldn't be totally lost: https://github.com/phatboyg/Magnum/tree/d73a9be8f53e03132614e5787c6b3bfcc875ed09/src/Magnum/FileSystem.

We would have issues with stuff across the network, sometimes if the share was on a SAN, or just randomly in general the FileSystemWatcher would stop completely. Never did figure it out, thus the above workaround.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top