C# App.config 自定义 配置节

本文详细介绍了如何使用 App.config 文件进行配置管理,并通过 XML 格式的配置节来定制应用的配置需求。包括直接读取配置节内容和通过自定义配置域、元素和集合的方式实现更灵活的配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 方法一、

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<configSections>
    
<section name="color"   type="System.Configuration.NameValueSectionHandler" />
    
<section name="message" type="System.Configuration.DictionarySectionHandler"/>
    
<section name="name"   type="System.Configuration.SingleTagSectionHandler"/>
  
</configSections>
  
<color>
    
<add key="red"   value="#ff0000"/>
    
<add key="green" value="#00ff00"/>
    
<add key="blue"  value="#0000ff"/>
  
</color>
  
<message>
    
<add key="welcome" value="你好,欢迎"/>
  
</message>
  
<name firstName="" lastName="明明"/>


</configuration> 

对于自定义的配置节,应该先在 <configSections>中声明要配置的节与类型

读取自定义配置节
 public static void Main(string[] args)
        {
            
//get color
            NameValueCollection color = (NameValueCollection)ConfigurationManager.GetSection("color");
            
foreach (String str in color.AllKeys) {
                Console.WriteLine(str
+":"+color[str]);
            }
            
//get message
            IDictionary message = (IDictionary)ConfigurationManager.GetSection("message");
            
foreach (String str in message.Keys) {
                Console.WriteLine(str
+":"+message[str]);
            }
            
// get name
            IDictionary name = (IDictionary)ConfigurationManager.GetSection("name");
            
foreach (String str in name.Keys)
            {
                Console.WriteLine(str 
+ ":" + name[str]);
            }   
            
//Console.WriteLine(name["firstName"]);
            Console.Read();
        } 

 方法二、通过ConfigurationSection【配置域】、ConfigurationElement【节点】、ConfigurationElementCollection【节点列表】实现自定义节

<configuration>
  <configSections>
  <section name="orders" type="ConsoleApplication4.OrdersSection, ConsoleApplication4"/>
  </configSections>
<orders companyID="2001">
  <order number="100001" amount="222.22">
  </order>
  <order number="300001" amount="33.33">
  </order>
  </orders>
</configuration>
App.config

下面我们要定义相应的实体对象,该实体对象中会有一个子对象【用来表示节点列表信息】(ConfigurationElementCollection)

namespace ConsoleApplication4
{
    public class OrdersSection : ConfigurationSection
    {
        [ConfigurationProperty("companyID", IsRequired = true)]
        public string CompanyID
        {
            get
            {
                return (string)base["companyID"];
            }
            set
            {
                base["companyID"] = value;
            }
        }

        [ConfigurationProperty("", IsDefaultCollection = true)]
        public OrderElementCollection Orders
        {
            get
            {
                return (OrderElementCollection)base[""];
            }
        }
    }
    public class OrderElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new OrderElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((OrderElement)element).Number;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "order";
            }
        }

        public OrderElement this[int index]
        {
            get
            {
                return (OrderElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    }
    public class OrderElement : ConfigurationElement
    {
        [ConfigurationProperty("number", IsRequired = true)]
        public string Number
        {
            get
            {
                return (string)base["number"];
            }
            set
            {
                base["number"] = value;
            }
        }

        [ConfigurationProperty("amount", IsRequired = true)]
        public double Amount
        {
            get
            {
                return (double)base["amount"];
            }
            set
            {
                base["amount"] = value;
            }
        }
    }
}
实现代码
OrdersSection config = (OrdersSection)ConfigurationManager.GetSection("orders");
            Console.WriteLine("CompanyId={0}",config.CompanyID);
            for (int i = 0; i < config.Orders.Count; i++)
            {
                Console.WriteLine("Amount={0},Number={1}", config.Orders[i].Amount, config.Orders[i].Number);
            }
读取自定义节

 

 

 

 

转载于:https://www.cnblogs.com/S-TGM/archive/2011/09/07/2170386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值