0

I posted a question here but deleted it after I found a rather tedious solution

I am trying to write an app that can monitor multiple folders. I looked at the following solutions: FIleSystemWatcher multiple folders (Dynamically) Create multiple instances of the same FileSystemWatcher Monitor multiple folders using FileSystemWatcher

I tried all their solutions. But what would happen is that it only worked for local folders. Not network drives.

My last implementation was a combination of all three:

public static void StartMultipleWatchers(string path)
{
    string[] paths = path.Split(',');

    foreach (string folderPath in paths)
    {
          try
          {
              string folderPathtrim = folderPath.Trim();
              WatchFile(folderPathtrim);
           }
           catch(Exception ex)
           {
               Logger.Error(ex);
           }
        }
    }

private static void WatchFile(string monitoredDir)
{
   FileSystemWatcher fsw = new FileSystemWatcher(monitoredDir, "*.gz");
   fsw.Changed += new FileSystemEventHandler(OnChanged);
   fsw.Created += new FileSystemEventHandler(OnCreated);
   fsw.EnableRaisingEvents = true;
   fsw.IncludeSubdirectories = true;
   Logger.Info($"Started loop Monitor of Folder: {monitoredDir}");
}

private static void OnCreated(object sender, FileSystemEventArgs e)
{
    FileInfo fileInfo = new FileInfo(e.FullPath);
    string value = $"Created: {e.FullPath}";
    Logger.Info(value);    
}

Again this solution only worked for local folder. Or if I only made one watcher that watched one network folder.

Then I tried this very tedious solution:

public static void TestManualWatchers()
{
   var fsw1 = new FileSystemWatcher(@"\\lap.org.com\tool_data_odp_ws\metrology\CIM\DATA_READY\");
   var fsw2 = new FileSystemWatcher(@"C:\TestPath\");
   fsw1.Changed += OnChanged;
   fsw1.Created += OnCreated;
   fsw1.EnableRaisingEvents = true;
   fsw1.IncludeSubdirectories = true;

   fsw2.Changed += OnChanged;
   fsw2.Created += OnCreated;
   fsw2.EnableRaisingEvents = true;
   fsw2.IncludeSubdirectories = true;    
   Logger.Info($"Started watching manual double files");

 }

Where I manually create each watcher and it's own properties. What is the difference between this tedious way and the dynamic above?

Is there a way to actually dynamically create individual watchers?

Charlieface
  • 52,284
  • 6
  • 19
  • 43
Datboydozy
  • 131
  • 7
  • The first version should have worked. What exactly are you seeing, are the events just not firing or are you getting an error? I note that you are not holding on to references of the watcher, which means they will probably get disposed at some point. – Charlieface Sep 15 '22 at 21:54
  • @Charlieface no events are being logged. Changes don't register. – Datboydozy Sep 15 '22 at 22:07
  • @Charlieface it only works for local drives. Not network – Datboydozy Sep 15 '22 at 22:12
  • File watchers don't work with all file systems. What kind of network drive? – Flydog57 Sep 15 '22 at 23:49
  • @Flydog57 the customer owns the drive I am not well versed with network drives. What do you mean "what kind of network drive?" what types of network drives are there? Maybe the answer to that question will help me ask the customer what type of drive they are using. – Datboydozy Sep 16 '22 at 00:42
  • Check out these two questions: [FileSystemWatcher not firing events](https://stackoverflow.com/questions/16278783/filesystemwatcher-not-firing-events) and [FileSystemWatcher stops catching events](https://stackoverflow.com/questions/6184115/filesystemwatcher-stops-catching-events). – Theodor Zoulias Sep 16 '22 at 02:05
  • Take a look at the accepted answer to this: https://stackoverflow.com/questions/151804/system-io-filesystemwatcher-to-monitor-a-network-server-folder-performance-con. All I remember is that we couldn't use the Win32 equivalent on some network drives way back in the 1990s (on NT 3.51 or NT 4) – Flydog57 Sep 16 '22 at 02:15

0 Answers0