Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txtfol1.Text
watchfolder.Filter = "*.*"
watchfolder.IncludeSubdirectories = True
'Add a list of Filters we want to specify, making sure
'you use OR for each Filter as we need to all of those
'Attributes The attributes of the file or folder
'CreationTime The time the file or folder was created
'DirectoryName The name of the directory
'FileName The name of the file
'LastAccess The date the file or folder was last opened
'LastWrite The date the file or folder last had anything written to it
'Security The security settings of the file or folder
'Size The size of the file or folder
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
NotifyFilters.Attributes
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
NotifyFilters.CreationTime
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
NotifyFilters.Size
' add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
' add the rename handler as the signature is different AddHandler watchfolder.Renamed, AddressOf logrename
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
btnStart.Enabled = False
btnStop.Enabled = True
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then txtfol1.Text &= "File " & e.FullPath & _
"has been modified" & vbCrLf
If e.ChangeType = IO.WatcherChangeTypes.Created Then txtfol1.Text &= "File " & e.FullPath & _
"has been created" & vbCrLf
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then txtfol1.Text &= "File " & e.FullPath & _
"has been deleted" & vbCrLf
End Sub
'This is the code for handling the Renamed event raised by the FileSystemWatcher class:
Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
txtfol1.Text &= "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
' Stop watching the folder
watchfolder.EnableRaisingEvents = False
watchfolder.IncludeSubdirectories = False
btnStart.Enabled = True
btnStop.Enabled = False
End Sub