C# follow folder junctions with FileSystemWatcher

988 views Asked by At

Is it possible to configure a FileSystemWatcher to watch other folders which are linked in with a folder junction point?

for example:

You are watching D:

You have D:\junction which points to E:\folder

When I create a file in E:\folder\file.txt I want to see it with the watcher as D:\junction\file.txt.

Is this possible?

Thanks.

3

There are 3 answers

1
m0s On

FileSystemWatcher is not supposed to monitor junctions or symlinks... and it monitors one folder at a time.

0
Raman Zhylich On

A workaround is to setup FileSystemWatcher for each subfolder including junction points.

private static void SetupFileSystemWatchers(string path, FileSystemEventHandler changedEventHandler)
{
  if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
  {
    var watcher = new FileSystemWatcher(path);
    watcher.IncludeSubdirectories = false;
    watcher.Changed += changedEventHandler;
    watcher.EnableRaisingEvents = true;

    foreach (var subDirectory in Directory.GetDirectories(path))
    {
      SetupFileSystemWatchers(subDirectory, changedEventHandler);
    }
  }