File Watcher

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.

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
 
I am not sure exactly what you are trying to do there. Just create the watchers, point them to a folder and they will raise events based on what happens, you just have to write methods that handle those events:

Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'code here
    End Sub
Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
Dim filename as string = e.FullPath
    End Sub
 
What I want to do is to make any pdf that has changed read only in the permissions. This is my second new script in vb.net, so I need a bit of guidance. IN the past with VBA, I tied code to a button and that was it.

The file system watcher needs to watch one folder path only, and set permissions to read only upon any change of a file.
 
You should be able to do that in the changed event. When a file is changed in the specified folder the event args will hold the filename information of the file that changed.

Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'code here
Dim filename as string = e.FullPath
If Filename.ToLower.EndsWith(".pdf") Then
'a pdf file was changed
End if
End Sub
 
Back
Top