【问题标题】:How to create custom config section in app.config如何在 app.config 中创建自定义配置部分
【发布时间】:2011-11-21 01:20:23
【问题描述】:

我想在 app.config 文件中添加自定义配置部分,如下所示

<Companies>
  <Company  name="" code=""/>
  <Company  name="" code=""/>
</Companies>

<Employees>
  <Employee name="" Des="" addr="" sal=""/>
  <Employee name="" Des="" addr="" sal=""/>
</Employeess>

<Departments>
  <Department Id="" Projects=""/>
</Departments>

<Projects>
  <Project Path=""/>
</Projects>

在部门部分中,它指的是项目部分。

谁能告诉我怎么做?以及如何在我的代码中访问它?

@Bhaskar:请找到您评论的代码。

 public class RegisterCompaniesConfig : ConfigurationSection
    {
        public static RegisterCompaniesConfig GetConfig()
        {
            return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies")?? new RegisterCompaniesConfig();
        } 
        [System.Configuration.ConfigurationProperty("Companies")]       
        public Companies Companies
        {
            get
            {
                object o = this["Companies"]; return o as Companies;
            }
        }
    } 

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

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

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



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

【问题讨论】:

  • 考虑将这些设置放在单独的 XML 文件中,而不是 app.config 中。
  • @Sjoerd ,将它们放在单独的配置文件中可以实现什么?这些条目都应该在一个文件中,因为它们是相关的。
  • @Geeta,在您上面的代码中,我看到了这个-ConfigurationManager.GetSection("RegisterCompanies")-您的配置文件中声明的RegisterCompanies 部分在哪里?根据您之前发布的内容,我只能看到&lt;section name="Companies"

标签: c#-4.0 app-config


【解决方案1】:

我所有的配置管理代码都基于我收集的 here 类。这是example,这是documentation。请注意,这是我个人从不再在线提供的博客文章中重构的代码。

【讨论】:

  • 我已经浏览了你的代码。但是你已经定义了你自己的类来处理像 ConfigElementCollectionBase 这样的集合。但是我想使用配置集合元素来做到这一点。如果你知道你能告诉我怎么写使用 ConfigurationElementCollection 的上述部分的代码。
【解决方案2】:

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

强烈推荐,写得很好,非常有帮助!我从那些优秀的文章中学会了如何处理自定义配置部分。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 我只是尝试了一个在一个网站中提到的简单示例。每当我执行 ConfigurationManager.GetSection("Companies") 时,它都会返回 null。谁能告诉我确切的问题是什么。
    • @Geeta ,你需要显示你的配置文件让我指出确切的问题。
    • 请找到配置文件内容
    • 您是否定义了处理程序类型“CustomConfigSectionTester.RegisterCompaniesConfig,CustomConfigSectionTester”?如果未定义,则可能不会加载相应的部分。
    • 是的,我添加了派生自 ConfigSection 的 RegisterCompaniesConfig 类。我还实现了 ConfigurationElementCollection 和 ConfigurationElement。
    猜你喜欢
    • 2010-11-21
    • 2011-10-02
    • 1970-01-01
    • 2012-10-04
    • 2015-07-05
    • 2011-05-25
    • 2012-01-28
    • 2016-05-02
    相关资源
    最近更新 更多