ConfigurationProperty 由于其保护级别而无法访问 [英] ConfigurationProperty is inaccessible due to its protection level

查看:42
本文介绍了ConfigurationProperty 由于其保护级别而无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

app.config 是这样的:

<预><代码><配置><configSections><section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/></configSections><AdWordsApi><add key="LogPath" value=".\Logs\"/>...</AdWordsApi></配置>

当我使用 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.

我用谷歌搜索了很长时间,我发现是使用 AppSettings,但我使用的是自定义部分..

谁能解释为什么会出现这个ConfigurationProperty is inaccessible"错误?谢谢

我已将SystemSystem.Configurationcopy local设置为true

解决方案

您可以使用 这篇文章.

你可以使用配置:

 <section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection"/></configSections><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();

您也可以使用这篇文章.

I wanna read/write (and save) application's configuration file in program

The app.config is like this:

<configuration>
  <configSections>
    <section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
  </configSections>
  <AdWordsApi>
    <add key="LogPath" value=".\Logs\"/>
    ...
  </AdWordsApi>
</configuration>

When I use ConfigurationManager.GetSection to read the app.config, it works:

var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);

But when I use ConfigurationManager.OpenExeConfiguration:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);

I always get this error:

'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level

But as I know, GetSection cannot save configuration at program runtime, Like I said at beginning: I wanna save configuration at program runtime, So I have to use OpenExeConfiguration.

I have googled for long time, what I found is to use AppSettings, but what I use is custom section..

Anyone could explain why this "ConfigurationProperty is inaccessible" error occured? Thanks

Edit:

I have set copy local of System and System.Configuration to true

解决方案

You can use this article.

Edit:

you can use config:

  <configSections>
    <section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection" />
  </configSections>
  <AdWordsApi.appSettings>
    <add key="LogPath" value=".\Logs\"/>
  </AdWordsApi.appSettings>

this code:

    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();

Also You can use this article.

这篇关于ConfigurationProperty 由于其保护级别而无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆