abmv
abmv

Reputation: 7108

App.Config Best Practices?

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?

Upvotes: 3

Views: 2856

Answers (2)

Stremlenye
Stremlenye

Reputation: 595

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;
      }
}

Upvotes: 1

Chris Becke
Chris Becke

Reputation: 36131

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.

Upvotes: 3

Related Questions