解决C#中FileSystemWatcher类的Changed事件触发多次的问题。
fsw = new FileSystemWatcher(System.Environment.CurrentDirectory + "\\conf\\","*.xml");
fsw.EnableRaisingEvents = true;
fsw.NotifyFilter = NotifyFilters.LastWrite;
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
首先设置NotifyFilter为LastWrite,这个就屏蔽了因为杀毒软件等各种外部因素导致Changed事件被触发。
然后设置它的EnableRaisingEvents属性如下:
- private void fsw_Changed(object sender,EventArgs e)
- {
- fsw.EnableRaisingEvents = false;
- LoadTreeViewData();
- fsw.EnableRaisingEvents = true;
- }
这样,先设置为false然后处理完文件之后再设置为true即可。