Search code examples
c#filesystemsfilesystemwatcher

FileWatcher not triggering event when a folder is added


I'm using FileSystemWatcher to get events when a folder content changes. I have managed to successfully watch for events when a file is added, deleted or renamed, but the event is not being triggered when a new folder is created within the directory that I'm watching.

I have tried with the following events:

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.Attributes;

I have also set the following two properties to be true, though it did not help

watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = true;

Is this possible to do with FileSystemWatcher or do I need to use something else?

Thanks


Solution

  • You haven't include Directories

    NotifyFilters.DirectoryName
    

    In your notifyfilter.

    See https://msdn.microsoft.com/en-us/library/system.io.notifyfilters(v=vs.110).aspx

    so you should setup your notifyfilter as follows:

    watcher.NotifyFilter = NotifyFilters.DirectoryName |
                           NotifyFilters.LastAccess | 
                           NotifyFilters.LastWrite | 
                           NotifyFilters.FileName | 
                           NotifyFilters.Size | 
                           NotifyFilters.Attributes;
    

    I didn't verify and you didn't mention it but having Filter set might filter out a diectory, although the docs speaks about Gets or sets the filter string used to determine what files are monitored in a directory.