Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Windows Forms

Advanced FileSystemWatcher

Rate me:
Please Sign up or sign in to vote.
4.56/5 (26 votes)
21 Sep 2006CPOL3 min read 197K   6.2K   106   40
This is an advanced manipulation of the FileSystemWatcher object.

Sample Image - fswatcherScreenshot.jpg

Introduction

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.

Problem

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.

Solution

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 :)

Peace and happy coding.

License

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


Written By
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 enjoy Woodworking, Fishing, Camping, and Hunting. I live on the west coast of Florida and enjoy spending my free time outdoors, or building stuff in my workshop.

Comments and Discussions

 
GeneralRe: FileCreated event issues Pin
Sven So.27-Sep-06 20:43
Sven So.27-Sep-06 20:43 
GeneralRe: FileCreated event issues Pin
Jon_Dylon27-Sep-06 22:18
Jon_Dylon27-Sep-06 22:18 
GeneralRe: FileCreated event issues Pin
Sven So.27-Sep-06 22:33
Sven So.27-Sep-06 22:33 
GeneralRe: FileCreated event issues Pin
Jon_Dylon27-Sep-06 22:36
Jon_Dylon27-Sep-06 22:36 
GeneralRe: FileCreated event issues Pin
VbManVzw17-May-07 13:13
VbManVzw17-May-07 13:13 
GeneralRe: FileCreated event issues Pin
Phil Smith31-Aug-07 15:07
Phil Smith31-Aug-07 15:07 
GeneralRe: FileCreated event issues Pin
Member 382859217-Feb-09 1:31
Member 382859217-Feb-09 1:31 
GeneralRe: FileCreated event issues Pin
Sven So.17-Feb-09 4:08
Sven So.17-Feb-09 4:08 
Interesting. I will try that out myself. Do I have to consider anything special or just moving
a big file in a subfolder?
GeneralRe: FileCreated event issues Pin
Member 382859217-Feb-09 18:12
Member 382859217-Feb-09 18:12 
GeneralRe: FileCreated event issues Pin
kensurferca10-Feb-10 11:15
kensurferca10-Feb-10 11:15 
Generalgood added functionality. but what about non ms NAS Pin
TVW-Dev25-Sep-06 11:02
TVW-Dev25-Sep-06 11:02 
GeneralRe: good added functionality. but what about non ms NAS [modified] Pin
Werner Rucker26-Sep-06 12:26
Werner Rucker26-Sep-06 12:26 
GeneralRe: good added functionality. but what about non ms NAS Pin
vegeta4ss21-May-09 10:40
vegeta4ss21-May-09 10:40 
QuestionHow about an event handler for the FileSystemWatcher.Error event? Pin
C-codist21-Sep-06 15:00
C-codist21-Sep-06 15:00 
AnswerRe: How about an event handler for the FileSystemWatcher.Error event? Pin
George Oakes22-Sep-06 1:20
professionalGeorge Oakes22-Sep-06 1:20 
GeneralRe: How about an event handler for the FileSystemWatcher.Error event? Pin
MarkAndrews4422-Jan-09 6:11
MarkAndrews4422-Jan-09 6:11 

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.