• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

C# FileWatcher connection problem

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.
1 Solution
Jacques Bourgeois (James Burger)PresidentCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now