C# FileWatcher connection problem

Posted on 2012-09-11
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

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now