码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-04-05 09:53:29      阅读:525      评论:0      收藏:0      [点我收藏+]

标签:sys   ceshi   obj   work   path   size   lte   false   change   

public static void WatchFile()
{

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "E:/Work/Test/source";
watcher.Filter = "*.txt";
watcher.NotifyFilter = NotifyFilters.Size;
watcher.Changed += new FileSystemEventHandler(Change_Watch);
watcher.EnableRaisingEvents = true;

}

public static void Change_Watch(object o, FileSystemEventArgs args)
{
string sourcePath = "E:/Work/Test/source/ceshi.txt";
string targetPath = "E:/Work/Test/target/ceshi.txt";
var watcher = o as FileSystemWatcher;
watcher.EnableRaisingEvents = false;
File.Copy(sourcePath, targetPath, true);
watcher.EnableRaisingEvents = true;

}

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

标签:sys   ceshi   obj   work   path   size   lte   false   change   

原文地址:http://www.cnblogs.com/sachem/p/6667120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!