Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of .Net's AppSettingsReader vs ConfigurationManager for reading application configuration settings

Is there a substantial difference between the AppSettingsReader class and the AppSettings member of the ConfigurationManager class in .Net 3.5?

I'm building out some legacy code and a previous developer used AppSettingsReader.GetValue(), whereas I am more prone to use ConfigurationManager.AppSettings.Get().

Looking at the internals, AppSettingReader seems to be more typesafe, but its usage seems slightly more verbose. If the app settings I'm retrieving are well-known and fairly static, is there any benefit to using AppSettingsReader?

like image 702
cori Avatar asked Aug 16 '10 22:08

cori


1 Answers

Not really. Internally AppSettingsReader just used the ConfigurationManager.AppSettings.

There is basically just a bit of fluff around checking that it exists, checking that what is going on is ok etc.

One thing is though that it will throw an exception when there is no element in the AppSettings..

    // From CTOR
    this.map = ConfigurationManager.AppSettings;

    ....
    public object GetValue(string key, Type type)

       ...

       string item = this.map[key];
       if (item == null)
       {
           throw new InvalidOperationException(SR.GetString("AppSettingsReaderNoKey", new object[] { key }));
       }

I think that most people will just use ConfigurationManager but the real answer is 'wahtever floats your boat'.

like image 119
Dave Walker Avatar answered Nov 05 '22 10:11

Dave Walker