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

发布于 2024-12-05 15:38:56 字数 2130 浏览 3 评论 0原文

我想在 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; } }
    } 

I want to add the custom configsection in the app.config file as follows

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

In the Department section it is referring to Projects section.

Can anybody tell me way to do it? And how to access it in my code?

@Bhaskar: Please find the code for your comment.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-12-12 15:38:56

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

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

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

Highly recommended, well written and extremely helpful! I've learned how to deal with custom config sections from those excellent articles.

木有鱼丸 2024-12-12 15:38:56

我的所有配置管理代码都基于我在此处收集的类。这是一个示例,这里有一些文档。请注意,这是我亲自从博客文章中重构的代码,该代码不再在线可用。

I base all my configuration management code on the classes I collected here. This is an example, and here's some documentation. Note that this is code I personally refactored from a blog post that isn't available on-line any more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文