Search code examples
c#.netwindowsfilesystemwatcher

.net FileSystemWatcher does not pick up moved folders


I have found plenty of discussions regarding moved files (which I have no problems with), but not any specific to moved folders (hence this post).

I have a FileSystemWatcher instantiated as follows:

  var fileWatcher = new FileSystemWatcher("C:\\mypath");
  fileWatcher.IncludeSubdirectories = true;
  fileWatcher.NotifyFilter = NotifyFilters.LastWrite
                               | NotifyFilters.FileName
                               | NotifyFilters.CreationTime
                               | NotifyFilters.Size;
   fileWatcher.Changed += OnChanged;
   fileWatcher.Created += OnChanged;
   fileWatcher.Deleted += OnDeleted;
   fileWatcher.Renamed += OnRenamed;
   fileWatcher.Error += WatcherOnError;
   fileWatcher.EnableRaisingEvents = true;

No matter what I do with files, I get the events raised as expected, however if I drag (move) in a folder (even with files in it) to the watched folder, none of the events are raised at all.

I am running on Windows 10 (not sure if other versions of win behave the same way).

Does anyone know how to get a notification for a folder move?


Solution

  • You're explicitly excluding directory changes by not including NotifyFilters.DirectoryName in your NotifyFilter.

    Here's a link to the documentation, but it's only correct by implication :-) I confirmed it by just using your code without and then with the extra flag.