Using FileSystemWatcher in VB.NET
By: Seth Hall
By: Seth Hall
So you’re wondering how you can use the FileSystemWatcher component in your applications eh? Well, it is quite easy. The FileSystemWatcher allows you to monitor file creations, deletions, errors, modified file and even file renames. The FileSystemWatcher allows you to set the Path to monitor, monitor certain extensions, and even set it up to monitor the subdirectories in the path you specified.
Before we begin, this tutorial is using a WinForms project but could be customized to do ConsoleApp. You will need to add in a FileSystemWatcher Component and a FolderBrowser Dialog.
Below is the layout of the Form1 and its components and their names.
Fsw1 – File System Watcher component
Fbd1 – Folder Browser Dialog
TextBox1 – Show you the path
Button1 – Browse button
ListBox1 – File Renames listbox
ListBox2 – File Creations listbox
ListBox3 – File Deletions listbox
ListBox4 – File Modifies listbox
The rest of this tutorial is fairly simple. You need to set the Properties for the FileSystemWatcher and the FolderBrowserDialog.
EnabledRaisingEvents – True
Filter - *.*
GenerateMember – True
IncludeSubdirectories – True
Modifiers – Friend
NotifyFilters - FileName, DirectoryName, LastWrite
We left Path blank because we will set it later. Now onto the FolderBrowserDialog:
RootFolder – MyComputer
For now that is all we need to worry about on the Folder Browser.
Now for the code for the Browse button. The code shows the user the folderbrowser dialog and allows them to set the path to monitor.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click fbd1.ShowDialog() TextBox1.Text = fbd1.SelectedPath fsw1.Path = TextBox1.Text MsgBox("Path to monitor is now set to: " & TextBox1.Text) End Sub
Now we can start to create the events for the FileSystemWatcher. Now on renames the sub for the FileSystemwatcher looks like this:
Private Sub fsw1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles fsw1.Renamed
For the sake of us using the e.FullPath to show the user the FileName that has been renamed we need to change the System.IO.RenamedEventArgs to System.IO.FileSystemEventArgs
Now the sub for handling File Renames:
Private Sub fsw1_Renamed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw1.Renamed ListBox1.Items.Add("File- " & e.FullPath.ToString & " renamed at: " & System.DateTime.Now) End Sub
Private Sub fsw1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw1.Created ListBox2.Items.Add("File- " & e.FullPath.ToString & " created at: " & System.DateTime.Now) End Sub
Private Sub fsw1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw1.Deleted ListBox3.Items.Add("File- " & e.FullPath.ToString & " deleted at: " & System.DateTime.Now) End Sub
And File Modified/Change:
Private Sub fsw1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw1.Changed ListBox4.Items.Add("File- " & e.FullPath.ToString & " modified at: " & System.DateTime.Now) End Sub
Now its your best bet to change the FileExtension in the FileSystemWatcher properties because you will be getting a lot of Modifications and File changes in the System32 Folder. But if you don’t mind those then its all good.
And that’s it, using the FileSystemWatcher is pretty simple for the most part. You now know how to monitor your system or a directory for any file changes that might take place.