【发布时间】:2012-01-25 03:13:59
【问题描述】:
我想在程序中读取/写入(并保存)应用程序的配置文件
app.config 是这样的:
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
当我使用 ConfigurationManager.GetSection 读取 app.config 时,它可以工作:
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);
但是当我使用 ConfigurationManager.OpenExeConfiguration 时:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);
我总是收到这个错误:
'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' 由于其保护级别而无法访问
但据我所知,GetSection 无法在程序运行时保存配置,就像我一开始说的:我想在程序运行时保存配置,所以我必须使用 OpenExeConfiguration .
我google了很久,发现是用AppSettings,但我用的是自定义部分..
谁能解释为什么会出现这个“ConfigurationProperty is inaccessible”错误?谢谢
编辑:
我已将 System 和 System.Configuration 的 copy local 设置为 true
【问题讨论】:
标签: c# .net app-config configurationmanager configurationsection