Click here to Skip to main content
12,447,961 members (69,707 online)
Click here to Skip to main content
Add your own
alternative version

Stats

65.1K views
2.2K downloads
45 bookmarked
Posted

DirectoryWatcher

, 27 Jul 2005
Rate this:
Please Sign up or sign in to vote.
DirectoryWatcher watches a chosen directory and its subdirectory for changes.

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

Share

About the Author

Winfried Wille
Software Developer Winfried Wille Softwareentwicklung
Germany Germany
I am a .Net programmer working since 25 years as software developer and system engineer. I live in Hamburg, Germany.
My application and blog are here http://w2ww.de .
Currently i work as freelancer.My hourly price is 40€. I gladly accept fixed price projects.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMultiple FileSystemWatcher trigger Pin
Lawrence Chee Hong Tan19-Mar-07 17:58
memberLawrence Chee Hong Tan19-Mar-07 17:58 
GeneralLogging changes to a text file. Pin
Satjit S24-Feb-07 0:32
memberSatjit S24-Feb-07 0:32 
Generalaggregatethe events Pin
Wrongway43218-May-06 10:51
memberWrongway43218-May-06 10:51 
GeneralException with a non-english date format Pin
MansonP24-Aug-05 6:06
memberMansonP24-Aug-05 6:06 
GeneralRe: Exception with a non-english date format Pin
Winfried Wille30-Aug-05 20:09
memberWinfried Wille30-Aug-05 20:09 
GeneralVB.NET Version Pin
Paul Roullier11-Aug-05 2:59
memberPaul Roullier11-Aug-05 2:59 
GeneralRe: VB.NET Version Pin
Winfried Wille11-Aug-05 7:33
memberWinfried Wille11-Aug-05 7:33 
Generaltake care Pin
UR-IT3-Aug-05 10:00
memberUR-IT3-Aug-05 10:00 
GeneralRe: take care Pin
Winfried Wille30-Aug-05 23:51
memberWinfried Wille30-Aug-05 23:51 
GeneralRe: take care Pin
Anonymous31-Aug-05 0:07
sussAnonymous31-Aug-05 0:07 
GeneralAccess folder detection through explorer Pin
Asif Rehman3-Aug-05 0:55
memberAsif Rehman3-Aug-05 0:55 
GeneralRe: Access folder detection through explorer Pin
Winfried Wille3-Aug-05 7:30
memberWinfried Wille3-Aug-05 7:30 
GeneralRe: Access folder detection through explorer Pin
Asif Rehman3-Aug-05 23:42
memberAsif 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.

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