Click here to Skip to main content
11,930,069 members (56,585 online)
Click here to Skip to main content
Add your own
alternative version


99 bookmarked

Advanced FileSystemWatcher

, 21 Sep 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an advanced manipulation of the FileSystemWatcher object.

Sample Image - fswatcherScreenshot.jpg


The Microsoft FileSystemWatcher class is pretty cool, I use it to see when new files are ready to be processed, and it works pretty good. However, the FileSystemWatcher does have its limitations. The one limitation that has caused me to write this code, and the article, is the FileSystemWatcher has no ability to tell the client that a folder does not exist anymore.

In one of my programs, I am watching a network folder. The program starts, and stays running 24x7x365. It is supposed to detect new files, and then other processes take the file and import or export it based on what type of file it is. The process of detecting files is mission critical, and needs to keep running and be able to recover after an error.

We had network problems last week, and the FileSystemWatcher has no current way of notifying the user or application that the folder/path we are watching is no longer available. And after the network path is restored, the FileSystemWatcher does not continue to watch the folders, and throws no errors or indication that the network path is unavailable, or that it has been restored.


When a network outage occurs, the FileSystemWatcher has no event that it fires to tell the application that the folder/path cannot be accessed. And, it has no way to recover once the network has been restored.


What I did was inherit the base FileSystemWatcher class, and add some custom events, a property, and a timer. By inheriting the base class, all the original functionality is retained. By adding events, a timer, and a property, we can use the timer_elapsed event to look and see if the network path exists. The property is an interval for the timer, so the user can tell the new class how often it should look to see if the network is still available. If the user does not specify an interval, the default 100 millisecond interval is used.

I added two new public events: NetworkPathAvailable and NetworkPathUnavailable. In code, the user will look in these events to determine if the network path is available, and the code can take the appropriate measures to continue working, or die gracefully.

The AdvancedFileSystemWatcher class can be used in .NET 1.1 or 2.0; the code I have provided is for VS2005 and .NET 2.0, but you can just use the class in either version of .NET.

The code also contains a real basic Windows Form, that has a folder path you can set, and a multiline textbox that will display the folder file events as they occur. You will also see how I am using the AdvancedFileSystemWatcher to gracefully kill the watcher when the network goes down, and how the application gracefully re-creates the watcher when the network is available again. You could add code to send an email to a system admin that the network went down, but that code is for you to deal with.

I appreciate any comment or questions about this code. I wrote this quickly one morning, so it is not fully tested, and I take no responsibility that the code works flawlessly. Use at your own risk Smile | :)

Peace and happy coding.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

George Oakes
Web Developer
United States United States
I have been developing software since 1995, and I enjoy it very much. My current position allows me to keep up with the latest technology, and it is refreshing.

I am married, and live with my wonderful wife, and our 2 lovely daughters.

I enjoy Woodworking, Fishing, Camping, and Auto racing. I live on the west coast of Florida and enjoy spending my free time outdoors, or building stuff in my workshop.

You may also be interested in...

Comments and Discussions

Generalexcellent! Pin
elitemarsher4-Aug-10 23:06
memberelitemarsher4-Aug-10 23:06 
GeneralGood Start For Me Pin
John Simmons / outlaw programmer13-Feb-10 10:55
memberJohn Simmons / outlaw programmer13-Feb-10 10:55 
GeneralRe: Good Start For Me Pin
truane14-Apr-15 4:57
membertruane14-Apr-15 4:57 
GeneralRe: Good Start For Me Pin
John Simmons / outlaw programmer15-Apr-15 1:34
memberJohn Simmons / outlaw programmer15-Apr-15 1:34 
GeneralMy vote of 1 Pin
Jammer21-Nov-09 8:44
memberJammer21-Nov-09 8:44 
GeneralError while trying to run project. The application has failed to start because the application configuration is incorrect... Pin
john_17268-Oct-09 12:19
memberjohn_17268-Oct-09 12:19 
NewsThings to know about FileSystemWatcher Pin
Glytzhkof13-Dec-08 9:27
memberGlytzhkof13-Dec-08 9:27 
GeneralRe: Things to know about FileSystemWatcher Pin
Glytzhkof27-Mar-09 4:53
memberGlytzhkof27-Mar-09 4:53 
GeneralRe: Things to know about FileSystemWatcher Pin
John Simmons / outlaw programmer13-Feb-10 7:22
memberJohn Simmons / outlaw programmer13-Feb-10 7:22 
GeneralRe: Things to know about FileSystemWatcher Pin
Glytzhkof13-Feb-10 8:42
memberGlytzhkof13-Feb-10 8:42 
GeneralWhere from the file is copied Pin
Member 454225720-Oct-08 1:42
memberMember 454225720-Oct-08 1:42 
Generalget the user Pin
techmind7864-Sep-08 1:19
membertechmind7864-Sep-08 1:19 
QuestionRe: get the user Pin
BobilProject26-Jan-09 6:11
memberBobilProject26-Jan-09 6:11 
GeneralThanks Pin
Tim M M22-Oct-07 17:25
memberTim M M22-Oct-07 17:25 
GeneralRe: Thanks Pin
muzzybee10-Dec-07 11:55
membermuzzybee10-Dec-07 11:55 
General_myTimer_Elapsed should check for EnableRaisingEvents Pin
WPKF9-Oct-07 19:20
memberWPKF9-Oct-07 19:20 
QuestionNeed help to track the source file Pin
dinoo@code20-Oct-06 1:24
memberdinoo@code20-Oct-06 1:24 
AnswerRe: Need help to track the source file Pin
George Oakes20-Oct-06 2:16
memberGeorge Oakes20-Oct-06 2:16 
GeneralRe: Need help to track the source file Pin
dinoo@code22-Oct-06 1:03
memberdinoo@code22-Oct-06 1:03 
GeneralFileCreated event issues Pin
Jon_Dylon27-Sep-06 0:43
memberJon_Dylon27-Sep-06 0:43 
GeneralRe: FileCreated event issues Pin
George Oakes27-Sep-06 2:53
memberGeorge Oakes27-Sep-06 2:53 
GeneralRe: FileCreated event issues Pin
Jon_Dylon27-Sep-06 5:40
memberJon_Dylon27-Sep-06 5:40 
GeneralRe: FileCreated event issues Pin
George Oakes27-Sep-06 5:45
memberGeorge Oakes27-Sep-06 5:45 
GeneralRe: FileCreated event issues Pin
stotti_no127-Sep-06 21:43
memberstotti_no127-Sep-06 21:43 
GeneralRe: FileCreated event issues Pin
Jon_Dylon27-Sep-06 23:18
memberJon_Dylon27-Sep-06 23:18 

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
Web01 | 2.8.151126.1 | Last Updated 21 Sep 2006
Article Copyright 2006 by George Oakes
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid