FileSystemWatcher not firing events
You seem to be creating the FileSystemWatcher as a local variable in the setup method. This will of course go out of scope at the end of the method and may well be getting tidied up at that point, thus removing the watches.
Try creating the FSW at a point where it will be persisted (eg a program level variable) and see if that sorts you out.
My problem was that I expected certain actions to cause the FileSystemWatcher
Changed
event to fire. For example, moving a file (clicking and dragging) from the desktop to the watched location did not raise an event but copying an existing file and pasting a new copy of it (there by creating a new file to the file system and not simply moving an existing one) caused the Changed
event to be raised.
My solution was to add every NotifyFilter
to my FileSystemWatcher
. This way I am notified in all cases where the FileSystemWatcher
is able to notify me.
NOTE that it isn't entirely intuitive/obvious as to which filters will notify you for specific cases. For example, I expected that if I included FileName
that I would be notified of any changes to an existing file's name...instead Attributes
seem to handle that case.
watcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Size |
NotifyFilters.Security;