我有以下代码,每当将新文件添加到 FileSystemWatcher 正在监视的目录中时,我想告诉我这些代码。问题是,当在被监控的目录中创建一个文件时,它会引发三个事件——我只想要一个。引发的事件都被“改变”了。有什么我做错了吗?
FileSystemWatcher _fileWatcher = new FileSystemWatcher();
_fileWatcher.Path = DirToMonitor;
_fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
_fileWatcher.Filter = "*.*";
_fileWatcher.Created += new FileSystemEventHandler(_fileWatcher_Created);
_fileWatcher.Renamed += new RenamedEventHandler(_fileWatcher_Renamed);
_fileWatcher.Changed += new FileSystemEventHandler(_fileWatcher_Changed);
_fileWatcher.EnableRaisingEvents = true;
编辑:我使用了“LastWrite”,因为这是在目录级别进行监控的,而不是新文件。唯一可能的变化似乎是 LastWrite、Size 和 Attributes。LastWrite 似乎是最明智的……