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


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;

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM