Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Initialize with the current directory and start the process. tbCurDirectory.Text = CurDir() SetupWatcher() End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents lbDirectory As System.Windows.Forms.ListBox Friend WithEvents btnChangeDir As System.Windows.Forms.Button Friend WithEvents DirectoryWatcher As System.IO.FileSystemWatcher Friend WithEvents tbCurDirectory As System.Windows.Forms.TextBox 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. _ Private Sub InitializeComponent() Me.tbCurDirectory = New System.Windows.Forms.TextBox() Me.lbDirectory = New System.Windows.Forms.ListBox() Me.btnChangeDir = New System.Windows.Forms.Button() Me.DirectoryWatcher = New System.IO.FileSystemWatcher() CType(Me.DirectoryWatcher, _ System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'tbCurDirectory ' Me.tbCurDirectory.Anchor _ = ((System.Windows.Forms.AnchorStyles.Bottom Or _ System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.tbCurDirectory.Location = New System.Drawing.Point(8, 184) Me.tbCurDirectory.Name = "tbCurDirectory" Me.tbCurDirectory.Size = New System.Drawing.Size(208, 20) Me.tbCurDirectory.TabIndex = 2 Me.tbCurDirectory.Text = "" ' 'lbDirectory ' Me.lbDirectory.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or _ System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.lbDirectory.Name = "lbDirectory" Me.lbDirectory.Size = New System.Drawing.Size(356, 173) Me.lbDirectory.TabIndex = 0 ' 'btnChangeDir ' Me.btnChangeDir.Anchor _ = (System.Windows.Forms.AnchorStyles.Bottom Or _ System.Windows.Forms.AnchorStyles.Right) Me.btnChangeDir.Location = New System.Drawing.Point(224, 184) Me.btnChangeDir.Name = "btnChangeDir" Me.btnChangeDir.Size = New System.Drawing.Size(120, 23) Me.btnChangeDir.TabIndex = 1 Me.btnChangeDir.Text = "Monitor Directory" ' 'DirectoryWatcher ' Me.DirectoryWatcher.EnableRaisingEvents = True Me.DirectoryWatcher.NotifyFilter _ = ((System.IO.NotifyFilters.FileName Or _ System.IO.NotifyFilters.DirectoryName) _ Or System.IO.NotifyFilters.LastWrite) Me.DirectoryWatcher.SynchronizingObject = Me ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(352, 213) Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.tbCurDirectory, Me.btnChangeDir, Me.lbDirectory}) Me.Name = "Form1" Me.Text = "Directory Monitor" CType(Me.DirectoryWatcher, System.ComponentModel. _ ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub btnChangeDir_Click _ (ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnChangeDir.Click SetupWatcher() End Sub Private Sub DirectoryChanged(ByVal sender As Object, _ ByVal e As System.IO.FileSystemEventArgs) _ Handles DirectoryWatcher.Changed, _ DirectoryWatcher.Created, DirectoryWatcher.Deleted FillList() End Sub Private Sub SetupWatcher() Try DirectoryWatcher.Path = tbCurDirectory.Text() DirectoryWatcher.EnableRaisingEvents = True FillList() Catch BadArg As ArgumentException lbDirectory.Items.Clear() lbDirectory.Items.Add("Bad Directory") End Try End Sub Private Sub FillList() lbDirectory.Items.Clear() lbDirectory.Items.AddRange _ (System.IO.Directory.GetFiles(DirectoryWatcher.Path)) End Sub End Class