Click here to Skip to main content
15,880,503 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 196.9K   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

 
Generalexcellent! Pin
elitemarsher4-Aug-10 22:06
elitemarsher4-Aug-10 22:06 
GeneralGood Start For Me Pin
#realJSOP13-Feb-10 9:55
mve#realJSOP13-Feb-10 9:55 
GeneralRe: Good Start For Me Pin
truane14-Apr-15 3:57
truane14-Apr-15 3:57 
GeneralRe: Good Start For Me Pin
#realJSOP15-Apr-15 0:34
mve#realJSOP15-Apr-15 0:34 
GeneralMy vote of 1 Pin
Jammer21-Nov-09 7:44
Jammer21-Nov-09 7:44 
GeneralError while trying to run project. The application has failed to start because the application configuration is incorrect... Pin
Roger C Moore8-Oct-09 11:19
Roger C Moore8-Oct-09 11:19 
NewsThings to know about FileSystemWatcher Pin
Glytzhkof13-Dec-08 8:27
Glytzhkof13-Dec-08 8:27 
GeneralRe: Things to know about FileSystemWatcher Pin
Glytzhkof27-Mar-09 3:53
Glytzhkof27-Mar-09 3:53 
GeneralRe: Things to know about FileSystemWatcher Pin
#realJSOP13-Feb-10 6:22
mve#realJSOP13-Feb-10 6:22 
GeneralRe: Things to know about FileSystemWatcher Pin
Glytzhkof13-Feb-10 7:42
Glytzhkof13-Feb-10 7:42 
GeneralRe: Things to know about FileSystemWatcher Pin
glytzhkof18-Jun-17 1:39
glytzhkof18-Jun-17 1:39 
GeneralWhere from the file is copied Pin
Anubhava Dimri20-Oct-08 0:42
Anubhava Dimri20-Oct-08 0:42 
Generalget the user Pin
techmind7864-Sep-08 0:19
techmind7864-Sep-08 0:19 
QuestionRe: get the user Pin
BobilProject26-Jan-09 5:11
BobilProject26-Jan-09 5:11 
GeneralThanks Pin
ZeusT22-Oct-07 16:25
ZeusT22-Oct-07 16:25 
GeneralRe: Thanks Pin
muzzybee10-Dec-07 10:55
muzzybee10-Dec-07 10:55 
General_myTimer_Elapsed should check for EnableRaisingEvents Pin
WPKF9-Oct-07 18:20
WPKF9-Oct-07 18:20 
QuestionNeed help to track the source file Pin
dinoo@code20-Oct-06 0:24
dinoo@code20-Oct-06 0:24 
AnswerRe: Need help to track the source file Pin
George Oakes20-Oct-06 1:16
professionalGeorge Oakes20-Oct-06 1:16 
GeneralRe: Need help to track the source file Pin
dinoo@code22-Oct-06 0:03
dinoo@code22-Oct-06 0:03 
GeneralFileCreated event issues Pin
Jon_Dylon26-Sep-06 23:43
Jon_Dylon26-Sep-06 23:43 
GeneralRe: FileCreated event issues Pin
George Oakes27-Sep-06 1:53
professionalGeorge Oakes27-Sep-06 1:53 
GeneralRe: FileCreated event issues Pin
Jon_Dylon27-Sep-06 4:40
Jon_Dylon27-Sep-06 4:40 
GeneralRe: FileCreated event issues Pin
George Oakes27-Sep-06 4:45
professionalGeorge Oakes27-Sep-06 4:45 
GeneralRe: FileCreated event issues Pin
Sven So.27-Sep-06 20:43
Sven So.27-Sep-06 20:43 
Another solution that I came up with is:

1) When the file creation event gets fired..
2) Open another filesystem watcher only on that file and wait for NotifyFilters.LastWrite
3) When LastWrite gets fired, he is done copying that file -> and you close the watcher for that file:

Big Grin | :-D

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.