Click here to Skip to main content
Licence 
First Posted 20 Apr 2002
Views 99,191
Bookmarked 45 times

A .NET File System Watcher

By | 20 Apr 2002 | Article
A utility that monitors a selected directory for changes

Sample Image - FSW1.jpg

Introduction

FSWatcher is a small utility that I wrote for a Distributed Objects class. No, FSWatcher doesn't know how to remote or anything like that! Instead, it just looks on the local drive space for the selected file and waits. Anytime anything changes, FSWatcher notifies the user by checking the box next to the item (if it exists!) and updating the label at the bottom of the dialog box.

This was a pretty simple project, once I knew what to look for. I found everything in the Visual Studio .NET help files. I don't know about you, but that's a first for me!

Sample Image

The meat of the code is here, where the filters and delegates are set up. The code is actually so easy that I won't insult your intelligence by stepping you through it. If you don't understand it, then use it as a guide and try to figure it out.
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = textBox1.Text;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 
                   NotifyFilters.DirectoryName | NotifyFilters.FileName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;
I had a lot of fun with this... I hope you do, too.

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

toneware

Software Developer (Senior)

United States United States

Member

Tony has a real job as a software Engineer. His interest in C# started with a homework assignment in a Distributed Objects course. Tony lives in New York state but hails from LA. At home on Solaris or Windows, he prefers Visual Development. C# and WPF rock!

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
GeneralMy vote of 1 Pinmemberanprox22:58 4 Sep '11  
GeneralCross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on PinmemberNileshUpadhyay20:36 4 Nov '09  
GeneralRe: Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on PinmemberMCY1:08 18 Jan '11  
GeneralHi... PinmemberNehal Pandya MCA19:06 12 Jun '09  
QuestionHow to determine drive types Pinmemberliuhoihing19:15 17 Nov '03  
GeneralNot working ! PinsussAnonymous7:35 29 Jun '03  
GeneralRe: Not working ! Pinmemberleppie7:47 29 Jun '03  
GeneralRe: Not working ! Pinmembermwdiablo3:20 1 Apr '04  
GeneralRe: Not working ! Pinmemberkadnan22:35 26 Sep '04  

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 21 Apr 2002
Article Copyright 2002 by toneware
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid