加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ConfigurationManager.GetSection和Configuration.GetSect

发布时间:2020-12-15 08:42:55 所属栏目:百科 来源:网络整理
导读:我正在尝试基于AppSettings创建自定义配置文件部分: configSections section name="customConfiguration" type="System.Configuration.AppSettingsSection,System.Configuration,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"//config
我正在尝试基于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.

This Stackoverflow回答建议我应该使用

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

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

解决方法

根据配置类 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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读