FileSystemWatcher
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MyDirectory";
watcher.Renamed += new RenamedEventHandler(watcher_Renamed)
watcher.EnableRaisingEvents = true;
只要C:\MyDirectory下的某个文件重命名都会运行watcher_Renamedwatcher.Path = @"C:\MyDirectory";
watcher.Renamed += new RenamedEventHandler(watcher_Renamed)
watcher.EnableRaisingEvents = true;
void watcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("File Renamed: Old Name: " + e.OldName +
" New Name: " + e.Name);
}
其他事件{
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);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Created += new FileSystemEventHandler(watcher_Created);
//只监视myFile.txt
watcher.Filter = "myFile.txt";
//监视扩展名为.txt
watcher.Filter = "*.txt";
watcher.Filter = "myFile.txt";
//监视扩展名为.txt
watcher.Filter = "*.txt";