Is there a way to know, when we get an "ONChanged" event
from the filesystemwatcher, what actually changed, file, last
access, last write, etc? Seems they are all bundled into the
one event?
In the Wrox book "Beginning Visual C#" 2nd ed, Ch 20, there's an
example (which you can download from
www.wrox.com) which actually
shows how to use this Class.
here's some of the relevant code:
this.watcher.EnableRaisingEvents = true;
this.watcher.SynchronizingObject = this;
this.watcher.Deleted += new
System.IO.FileSystemEventHandler(this.OnDelete);
this.watcher.Renamed += new
System.IO.RenamedEventHandler(this.OnRenamed);
this.watcher.Changed += new
System.IO.FileSystemEventHandler(this.OnChanged);
this.watcher.Created += new
System.IO.FileSystemEventHandler(this.OnCreate);
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
StreamWriter sw = new
StreamWriter("C:/FileLogs/Log.txt",true);
sw.WriteLine("File: {0} {1}", e.FullPath,
e.ChangeType.ToString());
sw.Close();
lblWatch.Text = "Wrote change event to log";
}
catch(IOException ex)
{
lblWatch.Text = "Error Writing to log";
}
}
public void OnRenamed(object source, RenamedEventArgs e)
{
try
{
StreamWriter sw =new
StreamWriter("C:/FileLogs/Log.txt",true);
sw.WriteLine("File renamed from {0} to {1}", e.OldName,
e.FullPath);
sw.Close();
lblWatch.Text = "Wrote renamed event to log";
}
catch(IOException ex)
{
lblWatch.Text = "Error Writing to log";
}
}
Hope you can use this to solve your problem.
Chris