Question about FileSystemWatcher

  • Thread starter Thread starter Becci
  • Start date Start date
B

Becci

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?
 
The event handler receives an argument of type FileSystemEventArgs containing
data related to this event.

The ChangeType property from the argument FileSystemEventArgs provide
information specific to this Change.




These are the Chage types you can get


Member name Description Value
All The creation, deletion, change, or renaming of a file or folder. 15
Changed The change of a file or folder. The types of changes include:
changes to size, attributes, security settings, last write, and last access
time. 4
Created The creation of a file or folder. 1
Deleted The deletion of a file or folder. 2
Renamed The renaming of a file or folder. 8
 
Hi,
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?

I'm afraid not. FileSystemWatcher is based on the Win32-API functions
Find{First|Next|Close}ChangeNotification that don't provide
the source of the notifications either.

bye
Rob
 
The ChangeType property from the argument FileSystemEventArgs provide
information specific to this Change.




These are the Chage types you can get


Member name Description Value
All The creation, deletion, change, or renaming of a file or folder. 15
Changed The change of a file or folder. The types of changes include:
changes to size, attributes, security settings, last write, and last access
time. 4
Created The creation of a file or folder. 1
Deleted The deletion of a file or folder. 2
Renamed The renaming of a file or folder. 8

You are not seeing he problem: I am getting the change event, that is O.K.!
I need to know not only that *something* has changed, I need to know
*what* has changed. According to your reply all the "types" of "changes"
have a value of 4. How can I know if the "last write" has changed or the
"last access" has changed, I don't, because they are both the same value,
i.e. 4, do you see the problem?
 
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
 
Becci,
Have you tried to use Multiple FileSystemWatcher objects?

One that monitors for LastWrite and a second that monitors for LastAccess, a
third that monitors for Size, a fourth...

FileSystemEventArgs almost needs another property along the lines of
FileSystemWatcher.NotifyFilters that indicates which filter raised the
event.

Hope this helps
Jay
 
Back
Top