我必须关注其中一个文件夹的更改。
class FileWatcher
{
FileSystemWatcher watcher = new FileSystemWatcher();
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public void StartWatching()
{
watcher.Path = AppDomain.CurrentDomain.BaseDirectory + "\\Read test data";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("Changes in folder: 'Read test data' ");
}
}
和初始化:
private FileWatcher watch = new FileWatcher();
private void StartReadCVSChanges_Click(object sender, EventArgs e)
{
watch.StartWatching();
}
出于某种原因,当我确实进行了一些更改时,事件处理程序被调用了 3 次:
我更喜欢它是一次。
有任何想法吗?