在App.config中C#中的自定义配置部分 [英] Custom Config section in App.config C#

查看:676
本文介绍了在App.config中C#中的自定义配置部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在C#

配置部分相当初学者我想创建配置文件自定义栏目。我一直在Google上搜寻后尝试是因为如下

的配置文件:

 <?XML版本=1.0编码=UTF-8>?; 
<结构>
< configSections>
< sectionGroup NAME =MyCustomSections>
<节名称=CustomSectionTYPE =CustomSectionTest.CustomSection,CustomSection/>
< / sectionGroup>
< / configSections>

< MyCustomSections>
< CustomSection键=默认/>
< / MyCustomSections>
< /结构>





CustomSection.cs

 命名空间CustomSectionTest 
{
公共类CustomSection:配置节
{
[的ConfigurationProperty( 键,默认值=默认,IsRequired = TRUE)]
[StringValidator(InvalidCharacters =〜!@#$%^&放大器; *(!)[] {} /;'\| \ \,MINLENGTH个= 1,最大长度= 60)]
公共字符串键
{
{返回此[钥匙]的ToString();}
集合{这个[钥匙] =值;}
}
}
}



当我使用这个代码来检索第一节得到一个错误说配置错误。

  VAR CF =(CustomSection)System.Configuration.ConfigurationManager.GetSection(CustomSection); 



我缺少什么?

感谢。


修改


我需要的最终是什么

 < CustomConfigSettings> 
<设定ID =1>
<添加键=名称值=N/>
<添加键=类型值=D/>
< /设置>
<设定ID =2>
<添加键=名称值=O/>
<添加键=类型值=E/>
< /设置>
<设定ID =3>
<添加键=名称值=P/>
<添加键=类型值=F/>
< /设置>
< / CustomConfigSettings>


解决方案

的App.config:

 <?XML版本=1.0编码=UTF-8>?; 
<结构>
< configSections>
< sectionGroup NAME =customAppSettingsGroup>
<节名称=customAppSettingsTYPE =System.Configuration.AppSettingsSection,System.Configuration,版本= 2.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
< / sectionGroup>
< / configSections>
< customAppSettingsGroup>
< customAppSettings>
<添加键=KeyOneVALUE =ValueOne/>
<添加键=KeyTwoVALUE =ValueTwo/>
< / customAppSettings>
< / customAppSettingsGroup>
< /结构>



用法:



  NameValueCollection中设置= 
ConfigurationManager.GetSection(customAppSettingsGroup / customAppSettings)
作为System.Collections.Specialized.NameValueCollection;

如果(!设置= NULL)
{
的foreach(在settings.AllKeys字符串键)
{
的Response.Write(键+: +设定[键] +< BR />中);
}
}



I'm a quite beginner with config sections in c#
I want to create a custom section in config file. What I've tried after googling is as the follows
Config file:

    <?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; }
        }
    }
}


When I use this code to retrieve Section I get an error saying configuration error.

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


What am I missing?
Thanks.

Edit
What I need ultimately is

    <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>

解决方案

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>

Usage:

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 />");
 }
}

这篇关于在App.config中C#中的自定义配置部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆