Duplicate Events with FileSystemWatcher

G

Guest

I am testing the FileSystemWatcher class for possible use in an application I
am going to be writing. I have a simple test application that lets met
browse to a folder and I am looking for a change event.

I have two problems. First, when a file is copied into the folder, four
change events fire. When I edit the file and save it, three change events
fire. I am working with plain text files being edited in NotePad. I really
just want to get one event that tells me when a file has been changed, or
when a new file is copied into the folder.

I am including the relavent code below. I would appreciate any suggestions
anyone has or insights into how the FileSystemWatcher class works.

private void cmdGetFolder_Click(object sender, System.EventArgs e)
{
this.folderBrowserDialog1.ShowDialog(this);
this.txtDirectoryToMonitor.Text = this.folderBrowserDialog1.SelectedPath;
FileSystemWatcher objWatcher = new FileSystemWatcher(
this.folderBrowserDialog1.SelectedPath);

objWatcher.NotifyFilter = NotifyFilters.LastWrite;
objWatcher.Changed += new FileSystemEventHandler(OnChanged);
objWatcher.EnableRaisingEvents = true;

}

private void OnChanged(object source, FileSystemEventArgs e)
{
ListViewItem objItem;

// Add the event to a list view control
// I was ignoring temp files when I tried this with Word documents
// but that is another problem.
if (e.FullPath.EndsWith(".tmp") == false)
{
objItem = new ListViewItem(e.FullPath);
objItem.SubItems.Add(e.ChangeType.ToString());
this.lvEvents.Items.Add(objItem);
}

}
 
G

Gabriel Lozano-Morán

From the MSDN Library:

Common file system operations might raise more than one event. For example,
when a file is moved from one directory to another, several OnChanged and
some OnCreated and OnDeleted events might be raised. Moving a file is a
complex operation that consists of multiple simple operations, therefore
raising multiple events. Likewise, some applications (for example, antivirus
software) might cause additional file system events that are detected by
FileSystemWatcher.

Your options:
1) Use a hashtable where you store the processed filenames and in the
OnChanged check if the filename is already in the hashtable and if it is
ignore it otherwise add it to the Hashtable. Then you can trigger an event
everytime you add to the hashtable;
2) Second option is to create your own FileSystemWatcher look-a-like that
will monitor a specified directory at certain intervals for changes;

If I had to implement this feature I would personally go for option 1.

Gabriel Lozano-Morán
http://www.realdn.net
 
R

Richard Grimes

Bill said:
I have two problems. First, when a file is copied into the folder,
four change events fire. When I edit the file and save it, three
change events fire. I am working with plain text files being edited
in NotePad. I really just want to get one event that tells me when a
file has been changed, or when a new file is copied into the folder.

Notepad is the culprit:

http://msdn2.microsoft.com/en-us/library/xcc1t119.aspx

"For example, if you use a FileSystemWatcher component to monitor the
creation of new files in a directory, and then test it by using Notepad
to create a file, you may see two Created events generated even though
only a single file was created. This is because Notepad performs
multiple file system actions during the writing process. Notepad writes
to the disk in batches that create the content of the file and then the
file attributes."

Richard
 
G

Guest

Thanks for the replies. They are very helpful. Evidently monitoring file
system changes is more complcated than I thought. I thnk I can handle the
scenarios where multiple events are generated, now that I understand it.

I have another problem with Word files, and probably other Office
applications as well. If I open a word file in the folder I am monitoring
and make changes and save it, I do not see any events on the word file
itself, but rather everything happens on a temp file that is created.
Furthermore, if I make further changes and save them no further events are
generated. Any thoughts on this or do you know where I can find more
detailed information about how Word writes data?
 

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