监视文件或文件夹
public class watcher
{
public static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("usage:Watcher.exe(directory)");
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[0];
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//Listenging the text files
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(onChanged);
watcher.Created += new FileSystemEventHandler(onChanged);
watcher.Deleted += new FileSystemEventHandler(onChanged);
watcher.Renamed += new RenamedEventHandler(onRenamed);
watcher.EnableRaisingEvents = true;
//waiting for quit
Console.WriteLine("Press\'q\' to quit the sample.");
while (Console.Read() != 'q') ;
Console.ReadKey();
}
public static void onChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine("file:" + e.FullPath + " " + e.ChangeType);
}
public static void onRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("File:{0} renamed to {1}",e.OldFullPath,e.FullPath);
}
}

浙公网安备 33010602011771号