由于其保护级别,ConfigurationProperty不可访问
本文关键字:ConfigurationProperty 访问 于其 保护 | 更新日期: 2023-09-27 18:18:24
我想读/写(和保存)应用程序的配置文件在程序
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不能在程序运行时保存配置,就像我一开始说的:我想在程序运行时保存配置,所以我必须使用 openexecconfiguration 。
我已经谷歌了很长时间,我发现是使用AppSettings,但我使用的是自定义部分..
谁能解释为什么这个"ConfigurationProperty is accessible"错误发生?由于
编辑:我设置了System和System的copy local。配置到true
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
你可以使用这篇文章。
编辑:可以使用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();
我不确定它是否会为你所要做的工作,但是你是否尝试过使用ConfigurationUserLevel。没有呢?