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

StackOverflow https://stackoverflow.com/questions/3497768

문제

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?

도움이 되었습니까?

해결책

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'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top