App.config C# 中的自定义配置部分

Posted

技术标签:

【中文标题】App.config C# 中的自定义配置部分【英文标题】:Custom Config section in App.config C# 【发布时间】:2012-10-04 08:32:56 【问题描述】:

我是 C# 中配置部分的初学者 我想在配置文件中创建一个自定义部分。我在谷歌搜索后尝试如下 配置文件:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="MyCustomSections">
      <section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/>
    </sectionGroup>
  </configSections>

  <MyCustomSections>
    <CustomSection key="Default"/>
  </MyCustomSections>
</configuration>

CustomSection.cs

    namespace CustomSectionTest

    public class CustomSection : ConfigurationSection
    
        [ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)]
        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]/;'\"|\\", MinLength = 1, MaxLength = 60)]
        public String Key
        
            get  return this["key"].ToString(); 
            set  this["key"] = value; 
        
    

当我使用此代码检索部分时,我收到一条错误消息,提示配置错误。

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection");

我错过了什么? 谢谢。编辑 我最终需要的是

    <CustomConfigSettings>
    <Setting id="1">
        <add key="Name" value="N"/>
        <add key="Type" value="D"/>
    </Setting>
    <Setting id="2">
        <add key="Name" value="O"/>
        <add key="Type" value="E"/>
    </Setting>
    <Setting id="3">
        <add key="Name" value="P"/>
        <add key="Type" value="F"/>
    </Setting>
</CustomConfigSettings>

【问题讨论】:

请查看以下问题:***.com/questions/3935331/…***.com/questions/7983127/custom-configurationsection***.com/questions/4738/… 【参考方案1】:

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="customAppSettingsGroup">
      <section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>
  </configSections>
  <customAppSettingsGroup>
    <customAppSettings>
      <add key="KeyOne" value="ValueOne"/>
      <add key="KeyTwo" value="ValueTwo"/>
    </customAppSettings>
  </customAppSettingsGroup>
</configuration>

用法:

NameValueCollection settings =  
   ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings")
   as System.Collections.Specialized.NameValueCollection;

if (settings != null)

 foreach (string key in settings.AllKeys)
 
  Response.Write(key + ": " + settings[key] + "<br />");
 

【讨论】:

对于任何想要让它快速工作的人。 3 件事:1. 你必须在你的引用中添加对 System.Configuration 的引用,2. 使用 System.Configuration; 3. 使用 System.Collections.Specialized; 您也可以根据需要省略组(customAppSettingsGroup)。【参考方案2】:

尝试使用:

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MyCustomSections/CustomSection");

您需要节组的名称和自定义节的名称。

【讨论】:

感谢您的回复,但这对我不起作用。请查看问题的编辑部分。【参考方案3】:

突出显示配置部分按 F1, 您将看到 MSDN 网站上的实现覆盖了一个名为“Properties”的属性,该属性返回一个“ConfigurationPropertyCollection”,因为您的属性具有该类型的匹配属性,如果不将它们包装起来,您应该能够使用您的属性填充此集合和 MS 的人一样。

【讨论】:

以上是关于App.config C# 中的自定义配置部分的主要内容,如果未能解决你的问题,请参考以下文章

App.Config 自定义配置部分问题

带有“添加”元素的简单列表的自定义 app.config 部分

您如何使用 c# 4.0 app.config 中的部分?

为啥使用 app.config 来存储配置数据?

在Web.config或App.config中的添加自定义配置

在Web.config或App.config中的添加自定义配置