的ConfigurationProperty无法访问由于其保护级别

编程入门 行业动态 更新时间:2024-10-26 05:19:02
本文介绍了的ConfigurationProperty无法访问由于其保护级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想读/写(保存)程序应用程序的配置文件

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

在app.config是这样的:

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>

当我使用 ConfigurationManager.GetSection 以读取在app.config,它的工作原理:

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"]);

但是,当我使用 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]   无法访问由于其保护级别

'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level

但我知道, GetSection 无法保存配置在程序运行时,就像我说的开头:我想保存在程序运行时配置的,所以我要使用 OpenExeConfiguration

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.

我用Google搜索了很长一段时间,我发现是使用的AppSettings,但我用的是自定义栏目。

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

任何人都可以解释为什么这个的ConfigurationProperty是无法访问错误发生?谢谢

Anyone could explain why this "ConfigurationProperty is inaccessible" error occured? Thanks

编辑:

我已经将复制本地的系统和 System.Configuration 的为真的

推荐答案

您可以使用的这篇文章。

编辑:

您可以使用配置:

<configSections> <section name="AdWordsApi.appSettings" type="System.Configuration.AppSettingsSection" /> </configSections> <AdWordsApi.appSettings> <add key="LogPath" value=".\Logs\"/> </AdWordsApi.appSettings>

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

你也可以使用这篇文章。

更多推荐

的ConfigurationProperty无法访问由于其保护级别

本文发布于:2023-11-26 20:00:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:于其   无法访问   级别   ConfigurationProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!