5,666,547 members and growing! (14,204 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate License: The Code Project Open License (CPOL)

Advanced FileSystemWatcher

By George Oakes

This is an advanced manipulation of the filesystemwatcher object
VB.NET 1.1, .NET 2.0, Win2K, WinXP, Windows, .NET, WinForms, Visual Studio, VS.NET2003, VS2005, Dev

Posted: 21 Sep 2006
Updated: 21 Sep 2006
Views: 40,987
Bookmarked: 44 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 4.19 Rating: 4.19 out of 5
1 vote, 10.0%
1
0 votes, 0.0%
2
1 vote, 10.0%
3
3 votes, 30.0%
4
5 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - fswatcherScreenshot.jpg

Introduction

The Microsoft FileSystemWatcher class is pretty cool, I use it to see when new files are ready to be proccessed, 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 the 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 notifiying 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, and 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 unavailable or available and the code can take the appropriate measures to continue working, or die gracefully.

The AdvancedFileSystemWatcher.vb 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


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 have a son who is 18 and is out on his own learning about life.

I enjoy Hunting, Fishing, Shooting, and Auto racing. I live on the west coast of Florida and love every minute of it. The only thing I would change would be to move to the Keys, and swim and fish for a living Smile


Occupation: Web Developer
Location: United States United States

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralWhere from the file is copiedmemberMember 45422571:42 20 Oct '08  
Generalget the usermembertechmind7861:19 4 Sep '08  
GeneralThanksmemberTim M M17:25 22 Oct '07  
GeneralRe: Thanksmembermuzzybee11:55 10 Dec '07  
General_myTimer_Elapsed should check for EnableRaisingEventsmemberWPKF19:20 9 Oct '07  
QuestionNeed help to track the source filememberdinoo@code1:24 20 Oct '06  
AnswerRe: Need help to track the source filememberGeorge Oakes2:16 20 Oct '06  
GeneralRe: Need help to track the source filememberdinoo@code1:03 22 Oct '06  
GeneralFileCreated event issuesmemberJon_Dylon0:43 27 Sep '06  
GeneralRe: FileCreated event issuesmemberGeorge Oakes2:53 27 Sep '06  
GeneralRe: FileCreated event issuesmemberJon_Dylon5:40 27 Sep '06  
GeneralRe: FileCreated event issuesmemberGeorge Oakes5:45 27 Sep '06  
GeneralRe: FileCreated event issuesmemberstotti_no121:43 27 Sep '06  
GeneralRe: FileCreated event issuesmemberJon_Dylon23:18 27 Sep '06  
GeneralRe: FileCreated event issuesmemberstotti_no123:33 27 Sep '06  
GeneralRe: FileCreated event issuesmemberJon_Dylon23:36 27 Sep '06  
GeneralRe: FileCreated event issuesmemberVbManVzw14:13 17 May '07  
GeneralRe: FileCreated event issuesmemberPhil Smith16:07 31 Aug '07  
Generalgood added functionality. but what about non ms NASmemberTVW-Dev12:02 25 Sep '06  
GeneralRe: good added functionality. but what about non ms NAS [modified]memberWerner Rucker13:26 26 Sep '06  
GeneralHow about an event handler for the FileSystemWatcher.Error event?memberT-luv16:00 21 Sep '06  
GeneralRe: How about an event handler for the FileSystemWatcher.Error event?memberGeorge Oakes2:20 22 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Sep 2006
Editor:
Copyright 2006 by George Oakes
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project