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

2023-09-03 16:27:46 作者:菊花碎大石

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

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

在app.config是这样的:

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>

当我使用 ConfigurationManager.GetSection 以读取在app.config,它的工作原理:

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"]);

但是,当我使用 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]   无法访问由于其保护级别

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

但我知道, GetSection 无法保存配置在程序运行时,就像我说的开头:我想保存在程序运行时配置的,所以我要使用 OpenExeConfiguration

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.

我用Google搜索了很长一段时间,我发现是使用的AppSettings,但我用的是自定义栏目。

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

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

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

编辑:

我已经将复制本地的系统和 System.Configuration 的为真的

推荐答案

您可以使用的这篇文章。

编辑:

您可以使用配置:

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

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

你也可以使用这篇文章。