Click here to Skip to main content
13,090,730 members (45,917 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 22 Mar 2004

File/Folder Watcher Wrapper

, 22 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Easy implmentation of folder watcher class, by wrapping the logic and events.


So you want to watch a folder for file changes? So, why not use the built in .NET file watcher class? It's extremely easy to use and implement. Only one problem: every time you need this functionality, you tend to write all the code all over again. So I thought: why not wrap the file watcher functionality and then just reuse the wrapper class.

Design of the Wrapper Class

The wrapper class contains all that is needed to watch a folder for changes:

  • it monitors changes / deletion / addition / renaming of files & folders
  • a filter can be supplied to only monitor a certain group of files, e.g.: *.txt.
  • you can start or stop the watcher on command.

The watcher also wraps the events fired by the .NET file watcher and fires a single event for all different types of file changes. This event uses the event arguments class: FileWatcherEventArgs. This class contains the filename, path, old filename (for renaming), old path (for renaming) and a change type. The change type is useful for monitoring what type of change occurred. It is an enum:

public enum fileWatcherChangeType

How to use the code

It's very easy. Add the file FileWatcher.cs to your project. Add the code "using Utils;" and that's it! Here is an example of some form code of how to use the class:

private void button1_Click(object sender, System.EventArgs e)
  Utils.FileWatcher fw = new Utils.FileWatcher(@"c:\test");

  fw.Changed += new Utils.FileWatcher.ChangedEventHandler(showChange);

private void showChange(object source, Utils.FileWatcherEventArgs e)
  MessageBox.Show(e.ChangeType.ToString() + " " + e.FileName);

There is also a public method destroyWatcher which stops the current file watcher.


That's it! Very basic and easy to implement. Enjoy


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

Brad Vincent
Web Developer
South Africa South Africa
I am a software developer at CoreBiz - a small but growing software developement house in the sleepy hollow of Pietermaritzburg in South Africa, specialising in custom software development for all businesses

You may also be interested in...

Comments and Discussions

Generalorey Pin
harsha_gns12-Apr-10 1:45
memberharsha_gns12-Apr-10 1:45 
QuestionCan this be used in a thread Pin
smesser12-Dec-05 7:11
membersmesser12-Dec-05 7:11 
AnswerRe: Can this be used in a thread Pin
Bhavesh Shah20-Dec-05 22:24
memberBhavesh Shah20-Dec-05 22:24 
GeneralRe: Can this be used in a thread Pin
smesser21-Dec-05 2:10
membersmesser21-Dec-05 2:10 
GeneralRe: Can this be used in a thread Pin
ibless12-Nov-07 8:47
memberibless12-Nov-07 8:47 
GeneralRe: Can this be used in a thread Pin
ibless12-Nov-07 8:51
memberibless12-Nov-07 8:51 
GeneralDouble event firing Pin
Kevin Alexandre18-Jul-04 12:35
memberKevin Alexandre18-Jul-04 12:35 
GeneralRe: Double event firing Pin
Sidhartha Shenoy26-Sep-05 20:47
memberSidhartha Shenoy26-Sep-05 20:47 
GeneralRe: Double event firing Pin
Bhavesh Shah20-Dec-05 22:22
memberBhavesh Shah20-Dec-05 22:22 
GeneralRe: Double event firing Pin
Sidhartha Shenoy21-Dec-05 1:19
memberSidhartha Shenoy21-Dec-05 1:19 
GeneralRe: Double event firing Pin
Bhavesh Shah21-Dec-05 1:33
memberBhavesh Shah21-Dec-05 1:33 
GeneralI want watch a file Pin
lonelywind19824-Jul-04 0:00
memberlonelywind19824-Jul-04 0:00 
GeneralRe: I want watch a file Pin
Spicket23-Nov-05 6:34
memberSpicket23-Nov-05 6:34 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 23 Mar 2004
Article Copyright 2004 by Brad Vincent
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid