如何删除FileSystemWatcher对象的特定实例 [英] How to delete a particular instance of FileSystemWatcher object
问题描述
我有一个Windows应用程序,该应用程序使用FileSystemWatcher监视文件的更改.可以添加多个文件位置,并为每个位置创建FileSystemWatcher的新实例,并将该位置添加到列表框中.有一个选项可以从列表框中删除位置.删除位置时,我需要删除/处理FileSystemWatcher的特定实例.有什么办法可以做到这一点?预先感谢.
I have a windows application which uses FileSystemWatcher to monitor the change of files. Multiple file locations can be added and for each location a new instance of FileSystemWatcher is created and the location is added to a listbox. There is an option to delete a location from the listbox. I need to delete/dispose the particular instance of FileSystemWatcher when a location is deleted. Is there any way to attain this? Thanks in advance.
FileSystemWatcher fsw;
private void CreateFWInstance(string strLoc)
{
if (strLoc != string.Empty)
{
fsw = new FileSystemWatcher();
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Path = strLoc;
fsw.SynchronizingObject = this;
fsw.EnableRaisingEvents = true;
}
}
推荐答案
好吧,您需要保留对该实例的引用,例如作为列表框条目中的 value ,或者是 Dictionary< string,FileSystemWatcher>
来从路径映射到观察者.然后,只需要处理观察者,并将其从字典中删除/删除列表项即可.
Well you'll need to keep a reference to the instance, e.g. as the value in the list box entry, or perhaps a Dictionary<string, FileSystemWatcher>
to map from path to watcher. Then just dispose of the watcher and remove it from the dictionary / remove the list item.
这篇关于如何删除FileSystemWatcher对象的特定实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!