Implementing a FileSystemWatcher as a Console Application...

Setting up a FileSystemWatcher is quite easy. Doing something with the events it fires is more of a challenge.


By: John Kilgo Date: May 2, 2004 Download the code.

A .NET FileSystemWatcher allows you to write an application that watches a physical directory for file system changes. Examples of file system changes are creating a file, renaming a file, deleting a file, etc. The FileSystemWatcher exists in the System.IO namespace which is imported into the module as shown in the top line of code listed below. This sample application has been created as a console application, meaning it runs from the command prompt in a command session as opposed to a normal windows client application.

We begin by creating a VB console application named 'Watcher' and adding a module also named 'Watcher'. We create a constant to hold the name of the directory to be watched. We've used 'c:\watchme' in the example. You will have to create this directory on your drive, or change the name in the module and recompile the project.

After that we must create handlers for the events that the FileSystemWatcher can implement. This is done using the AddHandler method as shown below. We also must set EnableRaisingEvents to True. The program will not function without this line. After that we use Console.WriteLine to issue some instructions to the user. Finally, we allow the user to exit the program simply by pressing Enter.

Imports System.IO

Module Watcher

  Const DirectoryName As String = "c:\watchme"
  Private intCreated As Integer

  Sub Main()
    Dim Watcher As New FileSystemWatcher(DirectoryName)
    Try
      ' Add delegates for events
      AddHandler Watcher.Created, New FileSystemEventHandler(AddressOf Watcher_Created)
      AddHandler Watcher.Changed, New FileSystemEventHandler(AddressOf Watcher_Changed)
      AddHandler Watcher.Deleted, New FileSystemEventHandler(AddressOf Watcher_Deleted)
      AddHandler Watcher.Renamed, New RenamedEventHandler(AddressOf Watcher_Renamed)
      ' Must do this
      Watcher.EnableRaisingEvents = True
      ' Display instructions and wait to exit
      Console.WriteLine("Make changes to the " & DirectoryName & "directory.")
      Console.WriteLine("You will see the events generated.")
      Console.WriteLine("Press ENTER to exit.")
      Console.ReadLine()
    Finally
      Watcher.Dispose()
    End Try
  End Sub

Next we must create the event handlers for the events FileSystemWatcher can handle. These are shown below.

  Private Sub Watcher_Created(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    Console.WriteLine("Event: File {0} {1}", e.FullPath, e.ChangeType)
    intCreated += 1
    If intCreated Mod 2 = 0 Then
      MsgBox("File Created: " & e.FullPath)
    End If
  End Sub

  Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    Console.WriteLine("Event: File {0} {1}", e.FullPath, e.ChangeType)
  End Sub

  Private Sub Watcher_Deleted(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    Console.WriteLine("Event: File {0} {1}", e.FullPath, e.ChangeType)
  End Sub

  Private Sub Watcher_Renamed(ByVal sender As Object, ByVal e As RenamedEventArgs)
    Console.WriteLine("Event: File {0} {1} to {2}", e.OldFullPath, e.ChangeType, e.FullPath)
  End Sub

End Module

A problem with the FileSystemWatcher occurs due to the way Windows implements file changes within a directory. If you run the program and then using notepad, or some other editor, create a file you will see within the command window that the Watcher_Created event is fired twice. Normally, you would want to take some action upon a file being created. All we have done, besides writing the events to the command window, is pop up a messagebox. The variable 'intCreated' is incremented and only on the second time the Created event fires do we pop up the messagebox. There may be a better way to deal with this problem.

We'll leave it up to you to figure out how you want to react to the various events, but hope the above sample will get you started in the right direction.

You may download the code here.