ConfigurationProperty 由于其保护级别而无法访问

发布于 2024-12-22 06:51:29 字数 1554 浏览 1 评论 0原文

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

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

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

任何人都可以解释为什么会发生这个“ConfigurationProperty 无法访问”错误?谢谢

编辑:

我已将SystemSystem.Configurationcopy local设置为true

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩发小公举 2024-12-29 06:51:30

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

I'm not sure if it will work for what you are trying to do, but have you tried using ConfigurationUserLevel.None instead?

难理解 2024-12-29 06:51:29
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
铁轨上的流浪者 2024-12-29 06:51:29

您可以使用本文

编辑:

您可以使用配置:

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

您也可以使用本文

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文