mathceleb
New member
- Joined
- May 17, 2011
- Messages
- 2
- Programming Experience
- 10+
I've been developing VBA and .php for most of my career, and have only had limited experience with vb.net. Below is the program I am working on. I wanted to be able to detect any file changes in one directory with regard to .pdf files. I found this sample online. I'm getting some errrors: For example, I need declarations on watchfolder, syntax errors on addhandler?
I got the basic syntax from here: http://www.devguru.com/features/tutorials/watchfolderact/watchfolder.asp
I built the forum with the two buttons and two inputs as instructed. I tied the code below to the Begin Watch button.
I got the basic syntax from here: http://www.devguru.com/features/tutorials/watchfolderact/watchfolder.asp
I built the forum with the two buttons and two inputs as instructed. I tied the code below to the Begin Watch button.
VB.NET:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
'This shall import the necessary class required for our application we also need to declare a public variable for our FileSystemWatcher class:
Public watchfolder As FileSystemWatcher
'Also add the following code to the 'btn_start_click' procedure:
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
'Add a list of Filters we want to specify, making sure
'you use OR for each Filter as we need to all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _ IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or_ IO.NotifyFilters.Attributes
' 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
btn_startwatch.Enabled = False
btn_stop.Enabled = True
'End of code for btn_start_click
Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then txt_folderactivity.Text &= "File " & e.FullPath & _
" has been modified" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then txt_folderactivity.Text &= "File " & e.FullPath & _
" has been created" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then txt_folderactivity.Text &= "File " & e.FullPath & _
" has been deleted" & vbCrLf
End If
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)
txt_folderactivity.Text &= "File" & e.OldName & _
" has been renamed to " & e.Name & vbCrLf
End Sub
'And lastly this is the code for the btn_stop_click, which shall stop the monitor:
' Stop watching the folder
watchfolder.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False
End Class