Click here to Skip to main content
Licence 
First Posted 22 Mar 2004
Views 75,018
Bookmarked 51 times

File/Folder Watcher Wrapper

By | 22 Mar 2004 | Article
Easy implmentation of folder watcher class, by wrapping the logic and events.

Introduction

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
{
fileAdded,
fileDeleted,
fileRenamed,
filechanged
}

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.StartWatcher();

  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.

Conclusion

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

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

Brad Vincent

Web Developer

South Africa South Africa

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalorey Pinmemberharsha_gns1:45 12 Apr '10  
QuestionCan this be used in a thread Pinmembersmesser7:11 12 Dec '05  
AnswerRe: Can this be used in a thread PinmemberBhavesh Shah22:24 20 Dec '05  
GeneralRe: Can this be used in a thread Pinmembersmesser2:10 21 Dec '05  
GeneralRe: Can this be used in a thread Pinmemberibless8:47 12 Nov '07  
GeneralRe: Can this be used in a thread Pinmemberibless8:51 12 Nov '07  
GeneralDouble event firing PinmemberKevin Alexandre12:35 18 Jul '04  
GeneralRe: Double event firing PinmemberSidhartha Shenoy20:47 26 Sep '05  
GeneralRe: Double event firing PinmemberBhavesh Shah22:22 20 Dec '05  
GeneralRe: Double event firing PinmemberSidhartha Shenoy1:19 21 Dec '05  
GeneralRe: Double event firing PinmemberBhavesh Shah1:33 21 Dec '05  
GeneralI want watch a file Pinmemberlonelywind19820:00 4 Jul '04  
GeneralRe: I want watch a file PinmemberSpicket6:34 23 Nov '05  

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.

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