C# – ConfigurationProperty is inaccessible due to its protection level


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

The app.config is like this:

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

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

var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");

But when I use ConfigurationManager.OpenExeConfiguration:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");

I always get this error:

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


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

Best Solution

string key_value = refconfig.AppSettings.Settings["key_name"].Value;
Related Question