ConfigurationProperty 由于其保护级别而无法访问
我想在程序中读/写(并保存)应用程序的配置文件
app.config 是这样的:
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
当我使用 ConfigurationManager.GetSection 读取 app.config 时,它有效:
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"]);
我总是收到此错误:
'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' 由于其保护级别而无法访问
但是据我所知,GetSection无法在程序运行时保存配置,就像我在开始时所说的那样:我想在程序运行时保存配置,所以我必须使用OpenExeConfiguration 。
我用谷歌搜索了很长时间,我发现是使用 AppSettings,但我使用的是自定义部分..
任何人都可以解释为什么会发生这个“ConfigurationProperty 无法访问”错误?谢谢
编辑:
我已将System和System.Configuration的copy local设置为true
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定它是否适合您想要做的事情,但是您是否尝试过使用 ConfigurationUserLevel.None 来代替?
I'm not sure if it will work for what you are trying to do, but have you tried using ConfigurationUserLevel.None instead?
您可以使用本文。
编辑:
您可以使用配置:
此代码:
您也可以使用本文。
You can use this article.
Edit:
you can use config:
this code:
Also You can use this article.