Mar*_* Ma 10 .net c# configurationmanager app-config configurationsection
我想在程序中读/写(并保存)应用程序的配置文件
app.config是这样的:
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我使用ConfigurationManager.GetSection读取app.config时,它可以工作:
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
但是当我使用ConfigurationManager.OpenExeConfiguration时:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
我总是得到这个错误:
由于其保护级别,'System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]'无法访问
但据我所知,GetSection无法在程序运行时保存配置,就像我在开头说的那样:我想在程序运行时保存配置,所以我必须使用OpenExeConfiguration.
我已经google了很长时间,我发现使用AppSettings,但我使用的是自定义部分..
任何人都可以解释为什么会出现"ConfigurationProperty无法访问"错误?谢谢
编辑:
我已复制本地的系统和System.Configuration至真
pca*_*ins 16
string key_value = refconfig.AppSettings.Settings["key_name"].Value;
Run Code Online (Sandbox Code Playgroud)
sin*_*ici 12
您可以使用本文.
编辑:
你可以使用配置:
<configSections>
<section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection" />
</configSections>
<AdWordsApi.appSettings>
<add key="LogPath" value=".\Logs\"/>
</AdWordsApi.appSettings>
Run Code Online (Sandbox Code Playgroud)
这段代码:
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();
Run Code Online (Sandbox Code Playgroud)
您也可以使用这篇文章.
| 归档时间: |
|
| 查看次数: |
26849 次 |
| 最近记录: |