问题是按照以下代码,将设置EnableRaisingEvents = false禁用所有或从触发创建事件?请指教
FileSystemWatcher watcher = new FileSystemWatcher(@"D:\Outgoing", "*.txt");
watcher.EnableRaisingEvents = false;
答案 0 :(得分:1)
EnableRaisingEvents
属性将帮助您启用或禁用引发事件。如果您将此属性设置为false,则它不会为所有操作引发任何事件,如果您不需要Created事件意味着您不需要订阅已创建的事件。这意味着观察者对象的定义如下:
FileSystemWatcher watcher = new FileSystemWatcher(@"D:\Sujith\Test folder");
watcher.Changed += new FileSystemEventHandler(watcher_changed);
watcher.Renamed += new RenamedEventHandler(watcher_renamed);
watcher.Deleted += new FileSystemEventHandler(watcher_deleted);
watcher.EnableRaisingEvents = true;
这实际上并没有禁用所创建的事件,它会引发事件,但我们还没有订阅它们
答案 1 :(得分:1)
我假设您不希望“创建”事件(可能是您希望通知其他事件,如“更改”,“已删除”,“已重命名...”)。如果是这样,您只能订阅所需的事件。如下所示,
FileSystemWatcher watcher = new FileSystemWatcher(@"D:\Outgoing", "*.txt");
watcher.Changed += new FileSystemEventHandler(OnChanged);
//watcher.Created += new FileSystemEventHandler(OnCreated); // not interested
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
...
希望这就是你要找的东西。