如何在app.config中创建自定义配置部分?

Emb*_*rja 90 c# app-config

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

<RegisterCompanies>
    <Companies>
      <Company name="Tata Motors" code="Tata"/>
      <Company name="Honda Motors" code="Honda"/>
    </Companies>
</RegisterCompanies>
Run Code Online (Sandbox Code Playgroud)

Jan*_*nda 144

创建ConfigurationElement公司:

using System.Configuration;
Run Code Online (Sandbox Code Playgroud)

ConfigurationElementCollection中:

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;
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

和ConfigurationSection:

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

    }
Run Code Online (Sandbox Code Playgroud)

然后你加载你的配置

<configuration>       
    <configSections>
          <section name="Companies" type="blablabla.RegisterCompaniesConfig" ..>
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,如果您使用的是MVC应用程序,那么列出的部分就可以了.使用控制台应用程序,Web服务以及其他人,您需要在'blablabla.RegisterCompaniesConfig'之后使用',AssemblyName' (19认同)
  • 永远不要让我感到惊讶Stackoverflow排名高于[供应商自己的文档网站](https://msdn.microsoft.com/en-us/library/2tw134k3.aspx)本身:) (8认同)
  • 对于不熟悉的人来说,还值得注意的是,&lt;configSections&gt; 需要成为 &lt;configuration&gt; 的第一个子级 (2认同)
  • 使 ConfigurationElementCollection 类型显式:https://www.codeproject.com/Tips/1037705/Make-Your-ConfigurationElementCollection-Type-Expl (2认同)

mar*_*c_s 9

您应该在CodeProject上查看Jon Rista关于.NET 2.0配置的三部分系列.

强烈推荐,写得很好,非常有帮助!

它非常清楚地向您展示了如何编写必要的类(派生自ConfigurationElement和/或ConfigurationSection)以便设计您需要的自定义配置部分.