using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher("C:\\");
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName |NotifyFilters.DirectoryName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.Error += new ErrorEventHandler(OnError);
fsw.EnableRaisingEvents = true;
Console.WriteLine("Press Enter to quit the sample.");
Console.ReadLine();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, wct.ToString());
}
private static void OnError(object source, ErrorEventArgs e)
{
Console.WriteLine("error");
if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
{
Console.WriteLine(("internal buffer overflow: " + e.GetException().Message));
}
}
}