Cannot Watch Folder using FileSystemWatcher in C#
I am attempting to monitor a folder (and its sub-folders) for changes. However, my handler event is neve开发者_运维技巧r executed. I am using the following code:
FileSystemWatcher m_Watcher = new FileSystemWatcher();
m_Watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Portal 2 Map Installer/";
m_Watcher.Filter = "";
m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
m_Watcher.IncludeSubdirectories = true;
m_Watcher.Changed += new FileSystemEventHandler(OnFolderChange);
m_Watcher.EnableRaisingEvents = true;
Help please!
Create a handler for the on error event and see what it says:
m_Watcher.Error += new ErrorEventHandler(OnError);
There are some notes on the Changed
event handler :
The Changed event is raised unexpectedly when a file is renamed, but is not raised when a directory is renamed. To watch for renaming, use the Renamed event.
So it would be prudent to handle the Renamed
event as well (at the least).
精彩评论