tags:

views:

176

answers:

2

Normally when you have a application configuration file in your application and your application is expected to read from it.

Is it good to check initially at start up if this file exists and raise an error and not to proceed at all ? (Worse case senarios)

Or leave it to the unhandled exception manager to handle it and shut down the application? (WPF/Winforms etc)

Please advice?

+1  A: 

A better approach would be to have sane defaults configured, and run in the absence of the file. Really, what happens if the file is present, but some critical setting has been deleted by the user?

Especially if the user can change config points using an in app UI, crashing out in the absence of the file leads to an unrecoverable situation.

Chris Becke
+1. app.config should be an *optional* method for altering the default configuration.
Jason Williams
This is true, but it's also useful to notify the user that the setting is missing, or else to log explicitly which settings are being used for this run. If they just made a small typo then silently ignoring their error could lead to a lot of frustration.
Mark Byers
A: 

You can use something like that, defining default values in code, but triing to read it from app.config:

private static int SomeValue
{
     get
     {
         int result = 60; //Some default value
         string str = ConfigurationManager.AppSettings["SomeValue"];
         if (!String.IsNullOrEmpty(str))
         {
             Int32.TryParse(str, out result);
         }
         return result;
      }
}
Stremlenye