What is the difference between ConfigurationManager.GetSection and Configuration.GetSection?

I am trying to create a section of a custom configuration file based on AppSettings:

<configSections> <section name="customConfiguration" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </configSections> 

When I tried to read it through ConfigurationManager.GetSection ("customConfiguration"), the returned object was of type System.Configuration.KeyValueInternalCollection. I was unable to read the values ​​of this collection, although I could see the keys, and I could not pass it to the AppSettingsSection.

This is Stackoverflow's answer suggesting to use

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection customSettingSection = (AppSettingsSection)config.GetSection("customConfiguration"); 

It worked. My question is: what is the difference between ConfigurationManager.GetSection () and Configuration.GetSection ()? When should I use one and when should I use the other?

+6
source share
1 answer

According to the MSDN documentation in the configuration class http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx ,

If your application needs read-only access to its own configuration, it is recommended to use the GetSection method overload for web applications. For the client application, use the GetSection method.

These methods provide access to cached configuration values ​​for the current application, which has better performance than the configuration class.

In particular, in client applications, ConfigurationManager retrieves the configuration file obtained by merging the application configuration file, the local user configuration file, and the roaming configuration file.

+5
source

Source: https://habr.com/ru/post/949950/


All Articles