How to delete a particular instance of FileSystemWatcher object

岁酱吖の 提交于 2021-02-08 08:23:25

问题


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;

        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/12119863/how-to-delete-a-particular-instance-of-filesystemwatcher-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!