ConfigurationProperty is inaccessible due to its protection level
Asked Answered
H

3

16

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

Hypoploid answered 21/12, 2011 at 9:30 Comment(0)
R
15

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.

Rockribbed answered 21/12, 2011 at 11:35 Comment(4)
I defined the AdwordsSettings as a subclass of ConfigurationSection as you said:pastecode.com/jF, and program stops here: string path = AdwordsSettings.Settings.LogPath with an NullReferenceException(Object not set to an instance of an object)Hypoploid
And when program stops there, I found AdwordsSettings.Settings is null in debuggerHypoploid
@gbstack, you should read that article in more details, or go through more similar articles to get better understanding of the configuration system, codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx. Your original error is expected, as you misunderstand how to read the section.Chaconne
@LexLi, Thanks, I'll read it. Exactly I know little about Configuration systemHypoploid
B
23
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
Borneo answered 3/4, 2015 at 0:5 Comment(2)
that's what fixed my code anyway... derived from this "Configuration.AppSettings returns an AppSettingSections object, AppSettingSections is derived from ConfigurationSection which is derived from ConfigurationElement which defines a this[] operator as "protected internal", which mean that it is "inaccessible due to its protection level." You might want to try cs.AppSettings.Settings["CompanyName"]);"Borneo
nice - worked for me, and saved me having to refactor my config settings. thanks.Bakst
R
15

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.

Rockribbed answered 21/12, 2011 at 11:35 Comment(4)
I defined the AdwordsSettings as a subclass of ConfigurationSection as you said:pastecode.com/jF, and program stops here: string path = AdwordsSettings.Settings.LogPath with an NullReferenceException(Object not set to an instance of an object)Hypoploid
And when program stops there, I found AdwordsSettings.Settings is null in debuggerHypoploid
@gbstack, you should read that article in more details, or go through more similar articles to get better understanding of the configuration system, codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx. Your original error is expected, as you misunderstand how to read the section.Chaconne
@LexLi, Thanks, I'll read it. Exactly I know little about Configuration systemHypoploid
L
1

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

Lamellirostral answered 21/12, 2011 at 10:6 Comment(1)
Thanks, but after using ConfigurationUserLevel.None and ConfigurationUserLevel.PerUserRoaming, I still get the same error..Hypoploid

© 2022 - 2024 — McMap. All rights reserved.