简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM