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
Mark ma
source share