Click here to Skip to main content
Click here to Skip to main content

DirectoryWatcher

By , 27 Jul 2005
Rate this:
Please Sign up or sign in to vote.

Introduction

DirectoryWatcher watches a chosen directory and its subdirectory for changes. When a change is detected it sets its Icon in the system tray to , 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 . 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.

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.

Technical

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 _
  NotifyFilters.Attributes)

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!

License

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

About the Author

Winfried Wille
Web Developer Winfried Wille Systemberatung
Germany Germany
I am a Dotnet programmer working since 23 years as software developer and system engineer. I live in Hamburg, Germany.
Currently i'm playing around with HTML 5 and JQuery.
My application and blog are here http://w2ww.de.
Currently i work as freelancer.My hourly price is currently discounted to 25€, normal is 40€.
Follow on   Twitter

Comments and Discussions

 
GeneralMultiple FileSystemWatcher trigger PinmemberLawrence Chee Hong Tan19-Mar-07 17:58 
GeneralLogging changes to a text file. PinmemberSatjit S24-Feb-07 0:32 
Generalaggregatethe events PinmemberWrongway43218-May-06 10:51 
GeneralException with a non-english date format PinmemberMansonP24-Aug-05 6:06 
GeneralRe: Exception with a non-english date format PinmemberWinfried Wille30-Aug-05 20:09 
GeneralVB.NET Version PinmemberPaul Roullier11-Aug-05 2:59 
GeneralRe: VB.NET Version PinmemberWinfried Wille11-Aug-05 7:33 
Generaltake care PinmemberUR-IT3-Aug-05 10:00 
GeneralRe: take care PinmemberWinfried Wille30-Aug-05 23:51 
GeneralRe: take care PinsussAnonymous31-Aug-05 0:07 
GeneralAccess folder detection through explorer PinmemberAsif Rehman3-Aug-05 0:55 
GeneralRe: Access folder detection through explorer PinmemberWinfried Wille3-Aug-05 7:30 
GeneralRe: Access folder detection through explorer PinmemberAsif Rehman3-Aug-05 23:42 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 28 Jul 2005
Article Copyright 2005 by Winfried Wille
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid