您的位置:首页 > 编程语言 > C#

解决C#中FileSystemWatcher类的Changed事件触发多次的问题。

2013-06-27 14:30 791 查看
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属性如下:

C#代码



private void fsw_Changed(object sender,EventArgs e) 
        { 
            fsw.EnableRaisingEvents = false; 
            LoadTreeViewData(); 
            fsw.EnableRaisingEvents = true; 
        } 
这样,先设置为false然后处理完文件之后再设置为true即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: