Click here to Skip to main content
Licence CPOL
First Posted 21 Sep 2006
Views 89,832
Downloads 1,069
Bookmarked 78 times

Advanced FileSystemWatcher

By | 21 Sep 2006 | Article
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)

About the Author

George Oakes

Web Developer

United States United States

Member

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.

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
Generalexcellent! Pinmemberelitemarsher22:06 4 Aug '10  
GeneralGood Start For Me PinmemberJohn Simmons / outlaw programmer9:55 13 Feb '10  
GeneralMy vote of 1 PinmemberJammer7:44 21 Nov '09  
GeneralError while trying to run project. The application has failed to start because the application configuration is incorrect... Pinmemberjohn_172611:19 8 Oct '09  
NewsThings to know about FileSystemWatcher PinmemberGlytzhkof8:27 13 Dec '08  
GeneralRe: Things to know about FileSystemWatcher PinmemberGlytzhkof3:53 27 Mar '09  
GeneralRe: Things to know about FileSystemWatcher PinmemberJohn Simmons / outlaw programmer6:22 13 Feb '10  
GeneralRe: Things to know about FileSystemWatcher PinmemberGlytzhkof7:42 13 Feb '10  
GeneralWhere from the file is copied PinmemberMember 45422570:42 20 Oct '08  
Generalget the user Pinmembertechmind7860:19 4 Sep '08  
QuestionRe: get the user PinmemberBobilProject5:11 26 Jan '09  
GeneralThanks PinmemberTim M M16:25 22 Oct '07  
GeneralRe: Thanks Pinmembermuzzybee10:55 10 Dec '07  
General_myTimer_Elapsed should check for EnableRaisingEvents PinmemberWPKF18:20 9 Oct '07  
QuestionNeed help to track the source file Pinmemberdinoo@code0:24 20 Oct '06  
AnswerRe: Need help to track the source file PinmemberGeorge Oakes1:16 20 Oct '06  
GeneralRe: Need help to track the source file Pinmemberdinoo@code0:03 22 Oct '06  
GeneralFileCreated event issues PinmemberJon_Dylon23:43 26 Sep '06  
GeneralRe: FileCreated event issues PinmemberGeorge Oakes1:53 27 Sep '06  
GeneralRe: FileCreated event issues PinmemberJon_Dylon4:40 27 Sep '06  
GeneralRe: FileCreated event issues PinmemberGeorge Oakes4:45 27 Sep '06  
GeneralRe: FileCreated event issues Pinmemberstotti_no120:43 27 Sep '06  
GeneralRe: FileCreated event issues PinmemberJon_Dylon22:18 27 Sep '06  
GeneralRe: FileCreated event issues Pinmemberstotti_no122:33 27 Sep '06  
GeneralRe: FileCreated event issues PinmemberJon_Dylon22:36 27 Sep '06  

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
Web03 | 2.5.120517.1 | Last Updated 21 Sep 2006
Article Copyright 2006 by George Oakes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid