Click here to Skip to main content
15,748,098 members
Articles / Programming Languages / Visual Basic
Posted 27 Jul 2005


45 bookmarked


Rate me:
Please Sign up or sign in to vote.
3.23/5 (9 votes)
27 Jul 20053 min read
DirectoryWatcher watches a chosen directory and its subdirectory for changes.


DirectoryWatcher watches a chosen directory and its subdirectory for changes. When a change is detected it sets its Icon in the system tray to Image 1, plays a sound, logs the change, and sets its state to “dirty”. If a file or directory is deleted, it logs the change, sets the state to be not “dirty” and sets the icon in the system Tray to Image 2. If a change is detected and the state is already dirty, no sound is played.

When you use the right mouse button on the tray icon, a context menu is displayed. By choosing “Properties” or double-clicking the tray icon, you go to the main window, where you see the log, make your settings, and reset the “dirty”-state.

Image 3

The log shows the result of copying the file 02_step01_int.jpg in and deleting the file yoda.bmp. As you see, three change events are logged for a single file that gets moved/copied in. This is because the current version doesn’t filter and aggregate the events, which come from the underlying Framework-class FileSystemWatcher, which reports changes in a directory. More about the generated and used events can be found in the Technical section. The tool does its work to report changes in a chosen directory, while generating very little load on the system. It is written with the Visual Studio 2005 Beta 2 using Visual Basic .NET. You need the .NET Framework 2.0 to run this application.


In this sample, you see the use of a lot of Framework-classes and usage of the new and handy My Namespace.

Subscribing for the wanted events of the FileSystemWatcher:

HotSpotFileSystemWatcher.NotifyFilter = _
  (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or _
  NotifyFilters.FileName Or NotifyFilters.DirectoryName Or _

The WatcherChangeTypes.Created and WatcherChangeTypes.All in the changed event of the FileSystemWatcher are never fired. This is bad since I subscribed to the Attribute change, to reset the dirty flag, when a file gets read. But changes to the file attributes as lastaccesstime doesn’t fire an event in the FileSystemWatcher class. The creation and renaming of a folder are not distinguishable, both generate a rename, followed by a change event on the new folder. When a file gets copied in the watched folder, usually three change events on the file are logged, followed by one change event for its directory (none if it’s the root folder).

The generation of new empty files only issues a change event on the containing folder. Saving a small text causes two change events on the file and one on the containing folder. The delete of a file fires immediately a single delete event, but it is followed by a delayed change event on the containing folder, for example, when you select the containing folder in the Explorer. A rename of a file issues a rename event on the file followed by a change event on it. This in this application is no problem, since both a rename and change cause the “dirty”-state. But under other circumstances a timer must be used to distinguish between a change and rename event.

You can choose to search at the beginning for changes after the last program termination. The creation-time is only updated when the file is copied in, but not when it’s moved. Currently I check for the file and directory creation date and also the directory write date. This unfortunately fires a directory write, when a files get deleted, a condition on which we want to clear the “dirty state”.

For hiding the main-window at start up a workaround was needed, because Visible=False has no effect in the Load event from the form and in the designer the Visible setting is missing.

Have fun!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer Winfried Wille Softwareentwicklung
Germany Germany
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

GeneralMultiple FileSystemWatcher trigger Pin
Lawrence Tan19-Mar-07 17:58
Lawrence Tan19-Mar-07 17:58 
GeneralLogging changes to a text file. Pin
Satjit24-Feb-07 0:32
Satjit24-Feb-07 0:32 
Generalaggregatethe events Pin
Wrongway43218-May-06 10:51
Wrongway43218-May-06 10:51 
GeneralException with a non-english date format Pin
MansonP24-Aug-05 6:06
MansonP24-Aug-05 6:06 
GeneralRe: Exception with a non-english date format Pin
Winfried Wille30-Aug-05 20:09
professionalWinfried Wille30-Aug-05 20:09 
GeneralVB.NET Version Pin
Paul Roullier11-Aug-05 2:59
Paul Roullier11-Aug-05 2:59 
GeneralRe: VB.NET Version Pin
Winfried Wille11-Aug-05 7:33
professionalWinfried Wille11-Aug-05 7:33 
Generaltake care Pin
UR-IT3-Aug-05 10:00
UR-IT3-Aug-05 10:00 
GeneralRe: take care Pin
Winfried Wille30-Aug-05 23:51
professionalWinfried Wille30-Aug-05 23:51 
GeneralRe: take care Pin
Anonymous31-Aug-05 0:07
Anonymous31-Aug-05 0:07 
GeneralAccess folder detection through explorer Pin
Asif Rehman3-Aug-05 0:55
Asif Rehman3-Aug-05 0:55 
Hi I'm working on an application which needs to connect to ftp server through virtual drive(created by me). The idea is that I want virtual drive to behave like ftp server explorer.

I've set all the events(created,changed,renamed,deleted) to synchronize all the files & folders. What I want is when user clicks specifice folder in my virtual drive. It gives me notifiction that specific folder has clicked or being accessed so that I can download particular folder's files into virtual drive.
GeneralRe: Access folder detection through explorer Pin
Winfried Wille3-Aug-05 7:30
professionalWinfried Wille3-Aug-05 7:30 
GeneralRe: Access folder detection through explorer Pin
Asif Rehman3-Aug-05 23:42
Asif Rehman3-Aug-05 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.