473,425 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Multiple events triggered by fileSystemWatcher

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();

}
May 11 '06 #1
1 10340
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.)

May 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Stanbrook | last post by:
I'm using the FileSystemWatcher to looks for new files arriving in a directory. Files are coming in via FTP and tend to be larger is size (> 1MB at least). I would like to fire an event once the...
2
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
4
by: Ken Madden | last post by:
I want to create multiple fileSystemWatchers in a Windows Service to constantly watch multiple folders for file creations and then execute certain code based on these actions. The problem is that...
5
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
5
by: zfeld | last post by:
I am serializing an object to XML and writing it to disk. whenever a change to my object occurs I call the save function to re-write to disk (see code below). I am monitoring the directory where...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
10
by: Kai Thorsrud | last post by:
Hi i'm an event Noob and i'm calling a class from my main module console app like this. I'm sorry if this is a lot of code to read but i can't see the error according to my book. Thanks a LOT. ...
12
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
1
by: D2 | last post by:
Hi, I have this problem when working with FileSystemWatcher class. I m using one of these objects to watch over a folder. Whenever i big file from another folder to the folder being watched, I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.