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.f ileSystemWatcherFTP)).BeginInit();
//
// fileSystemWatcherFTP
//
this.fileSystemWatcherFTP.EnableRaisingEvents = true;
this.fileSystemWatcherFTP.NotifyFilter =
((System.IO.NotifyFilters)((System.IO.NotifyFilter s.FileName |
System.IO.NotifyFilters.LastWrite)));
this.fileSystemWatcherFTP.Changed += new
System.IO.FileSystemEventHandler(this.fileSystemWa tcherFTP_Changed);
this.fileSystemWatcherFTP.Created += new
System.IO.FileSystemEventHandler(this.fileSystemWa tcherFTP_Create);
//this.fileSystemWatcherFTP.WaitForChanged(System.IO .WatcherChangeTypes.Created | System.IO.WatcherChangeTypes.Changed);
//
// ServiceFTP
//
this.ServiceName = "ServiceFTP";
((System.ComponentModel.ISupportInitialize)(this.f ileSystemWatcherFTP)).EndInit();
}