Tag: configurationsection

使用.Net配置框架加载具有所需子ConfigurationElement的ConfigurationSection

我有一个控制台应用程序试图从web.config文件加载CustomConfigurationSection。 自定义配置部分具有所需的自定义配置元素。 这意味着当我加载配置部分时,如果配置中不存在该配置元素,我希望看到exception。 问题是.NET框架似乎完全忽略了isRequired属性。 因此,当我加载配置部分时,我只创建自定义配置元素的实例并将其设置在配置部分。 我的问题是,为什么会发生这种情况? 我希望GetSection()方法触发ConfigurationErrorsexception,因为配置中缺少必需的元素。 这是我的配置部分的外观。 public class MyConfigSection : ConfigurationSection { [ConfigurationProperty(“MyConfigElement”, IsRequired = true)] public MyConfigElement MyElement { get { return (MyConfigElement) this[“MyConfigElement”]; } } } public class MyConfigElement : ConfigurationElement { [ConfigurationProperty(“MyAttribute”, IsRequired = true)] public string MyAttribute { get { return this[“MyAttribute”].ToString(); } } } 这是我加载配置部分的方法。 class Program { […]

每个配置文件只能出现一次! 为什么?

我得到以下例外:“每个配置文件只能出现一次。请参阅帮助主题以了解exception。” 我的配置文件如下所示 : 这是我加载它的代码 : public class PointServices : ConfigurationSection { public static PointServices Get() { var t = (PointServices)ConfigurationManager.GetSection(“point.Services/xServices”); return null; } // //Declares a collection element represented in the following configuration sub-section // // [ConfigurationProperty(“xServices”, IsDefaultCollection = true)] [ConfigurationCollection(typeof(PointServices), AddItemName = “xService”)] public PointServicesCollection Services { get { return (PointServicesCollection) base[“xServices”]; } } […]

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

我想在程序中读/写(并保存)应用程序的配置文件 app.config是这样的: … 当我使用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 。 我已经google了很长时间,我发现使用AppSettings,但我使用的是自定义部分.. 任何人都可以解释为什么会出现“ConfigurationProperty无法访问”错误? 谢谢 编辑: 我已将System和System.Configuration的 copy local设置为true

如何在.Net 2.0中的sectionGroup applicationSettings中按名称获取所有部分

这是我的想法: 我希望一个小的可执行文件有一个app.config文件,其中有多个部分位于sectionGroup“applicationSettings”下(不是“appSettings”,我不需要写入该文件)。 每个部分都有一个名称,对应于一个应该加载的模块。 这是一个例子: My awesome feature setting Some important string 现在,如果我定义FirstModule部分,我希望我的应用程序加载其程序集。 如果未定义该部分,则不应加载该模块。 这不仅适用于一个模块,也适用于尚未定义的模块。 所以我基本上需要在运行时找出已定义的部分。 我该怎么办? 此外,我希望它成为一个可移植的可执行文件(=它也必须在Mono上运行),它向后兼容.NET 2.0。 看看GitHub上的项目(目前在此提交中 )可能会很有趣。