Sim*_*wsi 6 .net c# configuration config
我正在尝试基于AppSettings创建自定义配置文件部分:
<configSections>
<section name="customConfiguration"
type="System.Configuration.AppSettingsSection,
System.Configuration,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
Run Code Online (Sandbox Code Playgroud)
当我尝试通过ConfigurationManager.GetSection("customConfiguration")读取它时,返回的对象是System.Configuration.KeyValueInternalCollection类型.我无法读取此集合的值,虽然我可以看到键,但我无法将其转换为AppSettingsSection.
这个 Stackoverflow的答案表明我应该使用
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection customSettingSection =
(AppSettingsSection)config.GetSection("customConfiguration");
Run Code Online (Sandbox Code Playgroud)
这很有效.我的问题是:ConfigurationManager.GetSection()和Configuration.GetSection()之间有什么区别?我何时应该使用另一个?何时应该使用另一个?
根据配置类的MSDN文档http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx,
如果您的应用程序需要对其自己的配置进行只读访问,则建议您对Web应用程序使用GetSection方法重载.对于客户端应用程序,请使用GetSection方法.
这些方法提供对当前应用程序的缓存配置值的访问,该配置值具有比Configuration类更好的性能.
具体来说,在客户端应用程序中,ConfigurationManager检索通过合并应用程序配置文件,本地用户配置文件和漫游配置文件获得的配置文件.
| 归档时间: |
|
| 查看次数: |
8279 次 |
| 最近记录: |