VB.Net - FileSystemWatcher

The FileSystemWatcher class is used to create a function that watches activities being performed on files sitting in a folder. When running it will throw an event whenever a file is added, changed, deleted, or renamed.

Actually I found several suprisingly good websites.

Note: I created a form, and placed the code within the form. I did this because If I created a main() routine, it would execute, and then terminate and the FileSystemWatcher would not be running. By creating the form I know that the FileSystemWatcher is functioning because the window is open.

On the form I added a textbox control:

  • A directory to watch named ‘txtDir’. it is populated with the folder being watched

Here is a stripped down version of the code behind the form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Imports System
Imports System.IO
Imports System.Diagnostics
Imports System.Windows.Forms

Public Class theMain
Public watchfolder As FileSystemWatcher

Private Sub theMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cmdline As String _
= Command()
Dim cmd() As String _
= cmdline.Split(" ")

Console.WriteLine("Directory Watch Utility")
Console.WriteLine("Creates a log of files added, modified, created, " _
& "deleted, or renamed for a specific directory")
Console.WriteLine("")
Console.WriteLine("Usage")
Console.WriteLine(" DirWatch DirectoryToWatch ")
Console.WriteLine("")
If cmd.Length < 2 Then
End
End If
txtDir.Text = cmd(0)

watchfolder = New System.IO.FileSystemWatcher()
watchfolder.Path = txtDir.Text
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
IO.NotifyFilters.Attributes
'AddHandler watchfolder.Changed, AddressOf logchange ' TMI
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler watchfolder.Renamed, AddressOf logrename
watchfolder.EnableRaisingEvents = True

Log("Dir Watcher Program Started - " & txtDir.Text)
End Sub


Private Sub logchange(ByVal source As Object _
, ByVal e As System.IO.FileSystemEventArgs)

If e.ChangeType = IO.WatcherChangeTypes.Changed Then
Log(e.FullPath & ", modified")
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
Log(e.FullPath & ", created")
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
Log(e.FullPath & ", deleted")
End If
End Sub
Public Sub logrename(ByVal source As Object _
, ByVal e As System.IO.RenamedEventArgs)

Log(e.OldName & ", renamed to " & e.Name)
End Sub

Public Sub Log(message As String)
Console.WriteLine(DateTime.Now.ToString & ", " + message)
End Sub

Private Sub theMain_FormClosing(sender As Object _
, e As FormClosingEventArgs) Handles Me.FormClosing
Log("Dir Watcher Program Ending - " & txtDir.Text)
End Sub
End Class

My full version sends the output to a log file - but that small bit of extra code was not needed for this example.