FileSystemWatcher behavior

G

Guest

I'm trying to get a FileSystemWatcher configured to watch for changes to only
one file. All I desire is for the "Changed" event to fire once every time
this file is written to. With the code I have now, the event is being fired
_four_ times when I only expect it once. Can anyone advise me on what may be
wrong?

Here is the code:

private FileSystemWatcher configureProcessListWatcher()
{
string processListPath =
ConfigurationSettings.AppSettings["ProcessListPath"];

FileInfo fi = new FileInfo(processListPath);

FileSystemWatcher watcher = new FileSystemWatcher(fi.DirectoryName,
fi.Name);

watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = true;

watcher.Changed += new FileSystemEventHandler(watcher_Changed);

return watcher;
}

---- END CODE -----

In my event handler, watcher_Changed, I open the file that is being watched.
Could this possibly trigger the event to fire again the way that I have this
set up?

Thanks for any help.
 
R

Robert Jordan

Hi Neils,
I'm trying to get a FileSystemWatcher configured to watch for changes to only
one file. All I desire is for the "Changed" event to fire once every time
this file is written to. With the code I have now, the event is being fired
_four_ times when I only expect it once. Can anyone advise me on what may be
wrong?

See

http://msdn.microsoft.com/library/d.../frlrfsystemiofilesystemwatcherclasstopic.asp

"Note: Common file system operations might raise more than one event."

bye
Rob
 
J

Jeff Gaines

I'm trying to get a FileSystemWatcher configured to watch for changes
to only one file. All I desire is for the "Changed" event to fire
once every time this file is written to. With the code I have now,
the event is being fired four times when I only expect it once. Can
anyone advise me on what may be wrong?
[snipped]

In my event handler, watcher_Changed, I open the file that is being
watched. Could this possibly trigger the event to fire again the way
that I have this set up?

Thanks for any help.

This can be confusing, as Robert says the event can fire more than
once.

What I did in one of my apps was to set a flag when the event(s) are
fired then check the flag with a timer. This saves re-acting to every
event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top