Multiple events triggered by fileSystemWatcher

G

Guest

Hi there,

I was trying to write a simple NT services using .NET 2.0 and
fileSystemWatcher control. The goal is to poll documents from a watch
directory and ftp them to a remote web site.

I created a Windows Services project using VS2005. Drag a fileSystemWatcher
control to the designer and added the following code. Here is what I found
out when I create a new file under the watch directory:
1) Using notepad - OnCreate event triggered 2 times. OnChange event
triggered 3 times.
2) Pipe a new file, for example, dir > c:\[watchdir]\temp.txt - OnCreate
event triggered 1 time. OnChange event triggered 3 times.

If I modify a file under the watch directory, Onchange event triggered 2
times.

I think the OnCreate event should work OK. It should be able to pick up new
file and just fire OnCreate event once. Inside the OnCreate event, I can FTP
the file to the site I want.

However, what if user wants to redrop an updated file with the same filename
to the watch directory? How can I reduce the OnChange event to only fire
once? I looked at the MSDN and it states that multiple events could be
triggered but it doesn't provide a solution.

Could you help? If I can't resolve this, I might need to use another
solution besides fileSystemWatcher control. I don't want to post the same
document 2 to 3 times for no reason.

Thanks.

Abel Chan

// Extracted code
private void InitializeComponent()
{
this.fileSystemWatcherFTP = new System.IO.FileSystemWatcher();

((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcherFTP)).BeginInit();
//
// fileSystemWatcherFTP
//
this.fileSystemWatcherFTP.EnableRaisingEvents = true;
this.fileSystemWatcherFTP.NotifyFilter =
((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName |
System.IO.NotifyFilters.LastWrite)));
this.fileSystemWatcherFTP.Changed += new
System.IO.FileSystemEventHandler(this.fileSystemWatcherFTP_Changed);
this.fileSystemWatcherFTP.Created += new
System.IO.FileSystemEventHandler(this.fileSystemWatcherFTP_Create);

//this.fileSystemWatcherFTP.WaitForChanged(System.IO.WatcherChangeTypes.Created | System.IO.WatcherChangeTypes.Changed);

//
// ServiceFTP
//
this.ServiceName = "ServiceFTP";

((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcherFTP)).EndInit();

}
 
L

Luke Zhang [MSFT]

Hello,

This is a known issue. By design, the framework is passing through the
native OS events, which may be any number of things, depending on the way
the file was changed/Created. A work around is to record last file name
"catched" by the control and exact "catch" time. If a second event has same
filename and similiar time, that event will be ignored.

Regards,

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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