c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?

前端之家收集整理的这篇文章主要介绍了c# – ConfigurationManager.GetSection和Configuration.GetSection有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试基于AppSettings创建自定义配置文件部分:
<configSections>
  <section name="customConfiguration" 
           type="System.Configuration.AppSettingsSection,System.Configuration,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>

当我尝试通过ConfigurationManager.GetSection(“customConfiguration”)读取它时,返回的对象是System.Configuration.KeyValueInternalCollection类型.我无法读取此集合的值,虽然我可以看到键,但我无法将其转换为AppSettingsSection.@H_404_5@

This Stackoverflow回答建议我应该使用@H_404_5@

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

这很有效.我的问题是:ConfigurationManager.GetSection()和Configuration.GetSection()之间有什么区别?我何时应该使用另一个?何时应该使用另一个?@H_404_5@

解决方法

根据配置类 http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx上的MSDN文档,

If your application needs read-only access to its own configuration,it is recommended that you use the 07001 method overloads for Web applications. For client application,use the 07002 method.@H_404_5@

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

具体来说,在客户端应用程序中,ConfigurationManager检索通过合并应用程序配置文件,本地用户配置文件和漫游配置文件获得的配置文件.@H_404_5@

猜你在找的C#相关文章