C# FileWatcher connection problem

Posted on 2012-09-11
Medium Priority
Last Modified: 2012-09-12
Basically, I have a program with multiple filewatchers in it that all connect to different directories over an internal network.  If all of the directories exist when the program starts, things go just fine.  However, if one of the directories doesn't exist (there are reasons this can happen in my situation) then I run into a bit of an issue.

I have put a try/catch in place to deal with the directory not existing and allowing the program to startup regardless of this problem.  However, once the program has started that filewatcher never has a chance to try and connect to the directory again.  I am not sure how to make my program monitor the directory without affecting the rest of the code which needs to be running at the same time (the other filewatchers that did connect).  I know I could check to see if the directory exists while setting up the filewatcher but I just don't know what to do with that information once I have it.  Any suggestions/help would be greatly appreciated.
Question by:MarkKristie
1 Comment
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 38389877
First of all, trapping errors is not the right way to correct the flow of a program. When you can, check for the condition instead. To detect a missing directory, use the static System.IO.Directory.Exists method. It requires far less resources than catching an exception.

A Try/Catch would be used only after you have connected, in case somebody deletes the directory while the application is running.

If I understand your question correctly, you would like to try to reconnect the unconnected FileWatcher later while the program is running. Simply use a Timer control or a Timer object set to fire every 1 or 2 minutes to check again.

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question