如何创建自定义的app.config配置节? [英] How to create custom config section in app.config?

查看:222
本文介绍了如何创建自定义的app.config配置节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的的app.config 文件中添加自定义的配置部分。
有没有办法做到这一点,我怎么能在我的程序访问这些设置。
以下是配置节我要添加到我的的app.config

 < RegisterCompanies>
    <公司与GT;
      <公司名称=塔塔汽车code =塔塔/>
      <公司名称=本田汽车code =本田/>
    < /公司和GT;
< / RegisterCompanies>


解决方案

创建的ConfigurationElement公司:

 公共类公司:的ConfigurationElement
{        [的ConfigurationProperty(名,IsRequired = TRUE)]
        公共字符串名称
        {
            得到
            {
                返回此[名称]作为字符串;
            }
        }
            [的ConfigurationProperty(code,IsRequired = TRUE)]
        公共字符串code
        {
            得到
            {
                返回此[code作为字符串;
            }
        }
}

ConfigurationElementCollection中:

 公共类公司
        :ConfigurationElementCollection中
    {
        公众公司这个[INT指数]
        {
            得到
            {
                返回base.BaseGet(指数)的公司;
            }
            组
            {
                如果(base.BaseGet(指数)!= NULL)
                {
                    base.BaseRemoveAt(索引);
                }
                this.BaseAdd(指数值);
            }
        }       大众新公司的[字符串responseString]
       {
            {返回(公司)BaseGet(responseString); }
            组
            {
                如果(BaseGet(responseString)!= NULL)
                {
                    BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
                }
                BaseAdd(值);
            }
        }        保护覆盖System.Configuration.ConfigurationElement CreateNewElement()
        {
            返回新公司();
        }        保护覆盖对象GetElementKey(System.Configuration.ConfigurationElement元)
        {
            返回((公司)元).Name点;
        }
    }

和配置节:

 公共类RegisterCompaniesConfig
        :配置节
    {        公共静态RegisterCompaniesConfig GetConfig()
        {
            返回(RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection(RegisterCompanies)?新RegisterCompaniesConfig();
        }        [System.Configuration.ConfigurationProperty(公司)]
            [ConfigurationCollection(typeof运算(公司),AddItemName =本公司)]
        大众公司公司
        {
            得到
            {
                对象o =本[公司];
                返回O作为公司;
            }
        }    }

你还必须注册在web.config中新的配置部分(的app.config):

 <结构>
    < configSections>
          <节名称=公司类型=blablabla.RegisterCompaniesConfig..>

然后你加载你的config

  VAR配置= RegisterCompaniesConfig.GetConfig();
的foreach(在config.Companies VAR项)
{
   做一点事 ..
}

I want to add a custom configuration section in my app.config file. Is there a way to do it and how can I access these settings in my program. Following is the config section I want to add to my app.config:

<RegisterCompanies>
    <Companies>
      <Company name="Tata Motors" code="Tata"/>
      <Company name="Honda Motors" code="Honda"/>
    </Companies>
</RegisterCompanies>

解决方案

Create ConfigurationElement Company:

public class Company :ConfigurationElement
{

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
        }
            [ConfigurationProperty("code", IsRequired = true)]
        public string Code
        {
            get
            {
                return this["code"] as string;
            }
        }
}

ConfigurationElementCollection:

public class Companies
        : ConfigurationElementCollection
    {
        public Company this[int index]
        {
            get
            {
                return base.BaseGet(index) as Company ;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

       public new Company this[string responseString]
       {
            get { return (Company) BaseGet(responseString); }
            set
            {
                if(BaseGet(responseString) != null)
                {
                    BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
                }
                BaseAdd(value);
            }
        }

        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new Company();
        }

        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            return ((Company)element).Name;
        }
    }

and ConfigurationSection:

public class RegisterCompaniesConfig
        : ConfigurationSection
    {

        public static RegisterCompaniesConfig GetConfig()
        {
            return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies") ?? new RegisterCompaniesConfig();
        }

        [System.Configuration.ConfigurationProperty("Companies")]
            [ConfigurationCollection(typeof(Companies), AddItemName = "Company")]
        public Companies Companies
        {
            get
            {
                object o = this["Companies"];
                return o as Companies ;
            }
        }

    }

and you must also register your new configuration section in web.config (app.config):

<configuration>       
    <configSections>
          <section name="Companies" type="blablabla.RegisterCompaniesConfig" ..>

then you load your config with

var config = RegisterCompaniesConfig.GetConfig();
foreach(var item in config.Companies)
{
   do something ..
}

这篇关于如何创建自定义的app.config配置节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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