FileSystemWatcher

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"C:\MyDirectory";
watcher.Renamed += new RenamedEventHandler(watcher_Renamed)
watcher.EnableRaisingEvents = true;
只要C:\MyDirectory下的某个文件重命名都会运行watcher_Renamed
void watcher_Renamed(object sender, RenamedEventArgs e)
{
  Console.WriteLine("File Renamed: Old Name: " + e.OldName +
    " New Name: " + e.Name);
}
其他事件
watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Created += new FileSystemEventHandler(watcher_Created);

//只监视myFile.txt
watcher.Filter = "myFile.txt";

//监视扩展名为.txt
watcher.Filter = "*.txt";

posted on 2008-02-28 08:28  zengshmin  阅读(495)  评论(0编辑  收藏  举报

导航