I found there are two ways to approach it, ReadDirectoryChangesW and FindFirstChangeNotification, I want to know what’s difference between them, performance or other???
+4
A:
This is tagged for Winapi, but I give it a shot with .net anyway.
You can use the FileSystemWatcher class from System.IO for this.
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher("C:\\");
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
Console.Read();
}
static void fsw_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("{0} was changed to {1}", e.OldName, e.Name);
}
static void fsw_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was deleted", e.Name);
}
static void fsw_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was created", e.Name);
}
static void fsw_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("{0} was changed", e.Name);
}
BeowulfOF
2009-01-06 09:03:49