我希望在程序中读取/写入(并保存)应用程序的配置文件
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.thisSystem.Configuration.ConfigurationProperty‘的保护级别,因此无法访问
但如我所知,GetSection不能在程序运行时保存配置,正如我一开始所说的:我想在程序运行时保存配置,所以我必须使用
我搜索了很长时间,找到的是使用AppSettings,但我使用的是自定义部分。
有人能解释为什么出现"ConfigurationProperty是不可访问的“错误吗?谢谢
编辑:
我已将复制系统的本地和System.Configuration设置为true
发布于 2011-12-21 11:35:59
您可以使用this article。
编辑:
您可以使用config:
<configSections>
<section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection" />
</configSections>
<AdWordsApi.appSettings>
<add key="LogPath" value=".\Logs\"/>
</AdWordsApi.appSettings>此代码:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
var settings = config.GetSection("AdWordsApi.appSettings") as AppSettingsSection;
if (settings != null) Console.Write(settings.Settings["LogPath"].Value);
Console.ReadLine();您也可以使用this article。
发布于 2015-04-03 00:05:18
string key_value = refconfig.AppSettings.Settings["key_name"].Value;发布于 2011-12-21 10:06:25
我不确定它是否适用于您想要做的事情,但是您是否尝试过使用ConfigurationUserLevel.None呢?
https://stackoverflow.com/questions/8587614
复制相似问题