File Watcher fires off 2 events?

ssalter

Member
Joined
Jul 13, 2005
Messages
5
Programming Experience
10+
RESOLVED : File Watcher fires off 2 events?

Long experienced in programming, web applications but new to .Net. I found an example of a file watcher application that I am trying to modify to use for an application. When a new file comes into a directory, I want to move it somewhere, open/parse and do db operations on it.

This code looks pretty simple and works well but when I add a file to the directory it is watching, I get two "file was created" messages. Any thoughts?
VB.NET:
Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
watchfolder = New System.IO.FileSystemWatcher
watchfolder.Path = txt_watchpath.Text
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
 
AddHandler watchfolder.Created, AddressOf logchange
 
watchfolder.EnableRaisingEvents = True
		btn_startwatch.Enabled = False
		btn_stop.Enabled = True
	End Sub
 
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
		If e.ChangeType = IO.WatcherChangeTypes.Created Then
			txt_folderactivity.Text &= "File " & e.FullPath & _
			" has been created" & vbCrLf
		End If
	End Sub
 
Private Sub txt_watchpath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_watchpath.TextChanged
	End Sub
	Private Sub txt_folderactivity_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_folderactivity.TextChanged
	End Sub
 
Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
		watchfolder.EnableRaisingEvents = False
		btn_startwatch.Enabled = True
		btn_stop.Enabled = False
	End Sub


Url I got example from is :http://www.developerfusion.com/show/3636/3/
 
Last edited:
You are instantiating the FSW in the Button's event handler. This means that each time you click the Start button you add a new FSW. If you click the button 10 times, you'll get 10 messages. Try adding your FSW in the designer instead of in code. You should always use the designer unless there is a specific reason not to. This means you have one class-level variable that refers to a single instance of the FileSystemWatcher class. Also, you get the functionality of the properties window and auto-generated event handlers.
 
I've read your code again, so let me clarify a few things. Your "watchfolder" variable is a class-level variable. I don't know whether you added it in the designer or declared it in code yourself, but either way you should only create one FSW object. This will be taken care of for you if you use the designer. If you dclare the variable yourself, you should create a new FSW once only, preferably in the Load event handler of the form. At the moment, you create a new FSW every time you click the Start button. Do not do this. Your "watchfolder" should already refer to an object if you have followed my advice up to now, so all you have to do is set its Enabled property to True.
 
Greetings, thanks for the info. It isn't my code, just something I found at the URL I quoted at the bottom of my original post. Your comments make perfect sense as to why I get multiple messages. When you say "designer" though, are you referring to the VS gui in design mode?

Regardless, I cut and pasted the code into the form's Load event and am still getting the two notices.


VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
watchfolder = New System.IO.FileSystemWatcher
watchfolder.Path = "c:\"
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
AddHandler watchfolder.Created, AddressOf logchange
watchfolder.EnableRaisingEvents = False
txt_watchpath.Text = "c:\"
End Sub
 
Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
btn_startwatch.Enabled = False
btn_stop.Enabled = True
watchfolder.EnableRaisingEvents = True
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Created Then
txt_folderactivity.Text &= "File " & e.FullPath & " has been created" & vbCrLf
End If
End Sub
 
Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
txt_folderactivity.Text &= "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf
End Sub
 
Private Sub txt_watchpath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_watchpath.TextChanged
watchfolder.Path = txt_watchpath.Text
End Sub
 
Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
watchfolder.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False
End Sub
 
I do mean the the VS gui in design mode. I'd suggest you get rid of the FSW variable declaration in your code, add an FSW to the form in the designer, set all your properties in the Properties window, then simply set the value of the EnableRaisingEvents property when your buttons are clicked. That should almost certainly clear up any issues.

You could even use a single button. You could change the button's Text depending on whether it is to start or stop, then you just do this:
VB.NET:
watchfolder.EnableRaisingEvents = Not watchfolder.EnableRaisingEvents
in the event handler.
 
Thanks...I haven't done windows apps for years, been focused on web apps. I found the control in the Tools menu. I didn't need that code I found on the net at all...its all here. :) Again, Thanks.
 
I found I was still getting multiple messages when I created a file there so after a bit MORE searching I found the following. However, the original suggestions were correct as well. When I stopped testing this thing via a text editor and just dropped files in my "watched" directory via Explorer, I only got one message.

FileSystemWatcher event is triggered twice (solution)

I've seen this behavior during a demo of mine today using notepad and a filesystemwatcher that monitors a single file. According to the Visual Studio .NET documentation (but you have to search a bit to find it): "This behavior is by design". The funny fact is that Notepad is in fact playing the evil role in this scenario. Let me paste this info here:
Multiple Created Events Generated for a Single Action

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. 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. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

Note Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.

For more information (if you have VS.NET installed with MSDN): ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtbsTroubleshootingUNCPathNamesNotAcceptedOnNT4Machines.htm
posted on Thursday, October 21, 2004 11:23 PM
 
Back
Top