PDA

View Full Version : FileSystemWatcher or FileWatcher


TheDude
09-15-2007, 09:38 AM
Here is a snippet of code for a filewatcher system


Private fsw As New FileSystemWatcher

Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
If Me.btnButton1.Text.ToLower = "watch" Then
AddHandler fsw.Created, AddressOf WatchHandler
AddHandler fsw.Changed, AddressOf WatchNameChange
Watch()
Me.btnButton1.Text = "Stop"
Else
StopWatch()
RemoveHandler fsw.Created, AddressOf WatchHandler
RemoveHandler fsw.Changed, AddressOf WatchNameChange
Me.btnButton1.Text = "Watch"
End If
End Sub

Private Sub Watch()
With fsw
.NotifyFilter = NotifyFilters.FileName
.Path = "\\yourFileServer\YourPath\"
.Filter = "*.txt"
.SynchronizingObject = Me
.EnableRaisingEvents = True
End With
End Sub

Private Sub StopWatch()
With fsw
.EnableRaisingEvents = False
End With
End Sub

Private Sub WatchHandler(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Me.rtbMain.Text &= e.ChangeType & ":" & vbTab & e.FullPath & vbCrLf
End Sub

Private Sub WatchNameChange(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Me.rtbMain.Text &= e.ChangeType & ":" & vbTab & e.FullPath & vbCrLf
End Sub