ConfigurationProperty unavailable due to security level

I want to read / write (and save) the application configuration file in the program

app.config is as follows:

<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 file, 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 unavailable due to the level of protection

But, as I know, GetSection cannot save configuration at runtime. As I said at the beginning: I want to save the configuration during program execution. Therefore, I need to use OpenExeConfiguration .

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

Can anyone explain why this "ConfigurationProperty was unavailable" error? Thanks

Edit:

I set copy local system and System.Configuration to true

+8
c # app-config configurationmanager configurationsection
source share
3 answers

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

You can also use this article .

+12
source share

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

+10
source share

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

+1
source share

All Articles