【问题标题】:ConfigurationProperty is inaccessible due to its protection levelConfigurationProperty 由于其保护级别而无法访问
【发布时间】:2012-01-25 03:13:59
【问题描述】:

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

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 .

我google了很久,发现是用AppSettings,但我用的是自定义部分..

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

编辑:

我已将 SystemSystem.Configurationcopy local 设置为 true

【问题讨论】:

    标签: c# .net app-config configurationmanager configurationsection


    【解决方案1】:
    string key_value = refconfig.AppSettings.Settings["key_name"].Value;
    

    【讨论】:

    • 这就是修复我的代码的原因...派生自此“Configuration.AppSettings 返回一个 AppSettingSections 对象,AppSettingSections 派生自 ConfigurationSection,它派生自 ConfigurationElement,它将 this[] 运算符定义为“受保护的内部",这意味着它“由于其保护级别而无法访问。”您可能想尝试 cs.AppSettings.Settings["CompanyName"]);"
    • 不错 - 为我工作,让我不必重构我的配置设置。谢谢。
    【解决方案2】:

    您可以使用this article

    编辑:

    你可以使用配置:

      <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

    【讨论】:

    • 我将 AdwordsSettings 定义为 ConfigurationSection 的子类,如您所说:pastecode.com/jF,程序在此处停止:字符串路径 = AdwordsSettings.Settings.LogPath 带有 NullReferenceException(对象未设置为对象)
    • 当程序停在那里时,我发现 AdwordsSettings.Settings 在调试器中是 null
    • @gbstack,您应该更详细地阅读那篇文章,或者阅读更多类似的文章以更好地了解配置系统,codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx。由于您误解了如何阅读该部分,因此预计会出现原始错误。
    • @LexLi,谢谢,我会读的。正是我对配置系统知之甚少
    【解决方案3】:

    我不确定它是否适用于您正在尝试做的事情,但您是否尝试过使用 ConfigurationUserLevel.None 代替?

    【讨论】:

    • 谢谢,但是在使用 ConfigurationUserLevel.None 和 ConfigurationUserLevel.PerUserRoaming 之后,我仍然得到同样的错误..
    猜你喜欢
    • 2015-09-26
    • 2011-09-01
    • 1970-01-01
    • 2011-04-02
    • 2011-02-07
    • 2022-01-18
    • 2019-06-28
    相关资源
    最近更新 更多