首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于ConfigurationProperty的保护级别,它无法访问。

由于ConfigurationProperty的保护级别,它无法访问。
EN

Stack Overflow用户
提问于 2011-12-21 09:30:30
回答 3查看 30K关注 0票数 14

我希望在程序中读取/写入(并保存)应用程序的配置文件

app.config是这样的:

代码语言:javascript
复制
<configuration>
  <configSections>
    <section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
  </configSections>
  <AdWordsApi>
    <add key="LogPath" value=".\Logs\"/>
    ...
  </AdWordsApi>
</configuration>

当我使用ConfigurationManager.GetSection读取app.config时,它是工作的:

代码语言:javascript
复制
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);

但是当我使用ConfigurationManager.OpenExeConfiguration

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-21 11:35:59

您可以使用this article

编辑:

您可以使用config:

代码语言:javascript
复制
  <configSections>
    <section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection" />
  </configSections>
  <AdWordsApi.appSettings>
    <add key="LogPath" value=".\Logs\"/>
  </AdWordsApi.appSettings>

此代码:

代码语言:javascript
复制
    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

票数 14
EN

Stack Overflow用户

发布于 2015-04-03 00:05:18

代码语言:javascript
复制
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
票数 20
EN

Stack Overflow用户

发布于 2011-12-21 10:06:25

我不确定它是否适用于您想要做的事情,但是您是否尝试过使用ConfigurationUserLevel.None呢?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8587614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档