VB.Net FileSystemWatcher Fire Multiple Times

  • Thread starter Thread starter jimbrown.net
  • Start date Start date
J

jimbrown.net

Guest
I have this code and if I btn_startwatch everything works great fires only once. if I start and stop muiltiple time I get a fire for each start. your thoughts on what's wrong? also the only Handler that seems to work is the Change. Created, Deleted and Rename don't work.


Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click

If txt_watchpath.Text = "" Then
MsgBox("Must enter a Watch Folder!", vbExclamation, "Oops!")
Exit Sub
End If

If System.IO.Directory.Exists(txt_watchpath.Text) = False Then
MsgBox("Watch Folder, Does NOT Exist!", vbExclamation, "Oops!")
Exit Sub
End If

LabelFileExtension.Enabled = False
CheckBoxFileExtension.Enabled = False
TextBoxFileExtension.Enabled = False

txt_watchpath.Enabled = False
BtnFolderBrowser.Enabled = False
BtnLog.Enabled = False

StartWatch()
btn_stop.Focus()

End Sub


Private Sub StartWatch()
'this is the path we want to monitor
FileSystemWatcher.Path = txt_watchpath.Text
'FileSystemWatcher.Filter = "*." & TextBoxFileExtension.Text

'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those

FileSystemWatcher.NotifyFilter = NotifyFilters.DirectoryName
FileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
FileSystemWatcher.NotifyFilter = NotifyFilters.Size
FileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite
FileSystemWatcher.NotifyFilter = NotifyFilters.FileName
FileSystemWatcher.NotifyFilter = NotifyFilters.Attributes

' add the handler to each event
AddHandler FileSystemWatcher.Changed, AddressOf logchange
AddHandler FileSystemWatcher.Created, AddressOf logchange
AddHandler FileSystemWatcher.Deleted, AddressOf logchange

' add the rename handler as the signature is different
AddHandler FileSystemWatcher.Renamed, AddressOf logrename

'Set this property to true to start watching
FileSystemWatcher.EnableRaisingEvents = True

btn_startwatch.Enabled = False
btn_stop.Enabled = True

End Sub

Private Sub btn_stopwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click

LabelFileExtension.Enabled = True
CheckBoxFileExtension.Enabled = True
TextBoxFileExtension.Enabled = True

txt_watchpath.Enabled = True
BtnFolderBrowser.Enabled = True
BtnLog.Enabled = True

'Stop watching the folder
FileSystemWatcher.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False

End Sub


Lastly if I try and change the FileSystemWatcher.IncludeSubdirectories with a checkbox I get ArgumentException was unhandled "The path is not of a legal form."


Private Sub CheckBoxFileExtension_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxFileExtension.CheckedChanged

If CheckBoxFileExtension.Checked = True Then
TextBoxFileExtension.Enabled = True
End If

If CheckBoxFileExtension.Checked = False Then
TextBoxFileExtension.Enabled = False
End If

End Sub













Work Smarter Not Harder

Continue reading...
 
Back
Top