web.config C#中使用自定义配置

web.config C#中使用自定义配置


         //配置节声明区域,包含配置节和命名空间声明
        

               //配置节声明
                    //定义配置节组
        
        //配置节组中的配置节声明
    
     //预定义配置节
       //配置节设置区域

 

 

自定义配置可分为:自定义配置节和自定义配置节组。

 

1. 自定义配置节

要使用自定义配置需要修改两处地方,一是在中声明配置节,二是在处设置配置节的具体配置。

声明配置节语法:

 

 

 

:声明新配置节

name:自定义配置节的名称

type: 自定义配置节的类型,主要包括System.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler。

SingleTagSectionHandler 配置节返回类型为 Systems.Collections.IDictionary
DictionarySectionHandler 配置节返回类型为 Systems.Collections.IDictionary
NameValueSectionHandler 配置节返回类型为 Systems.Collections.Specialized.NameValueCollection

 

下面是一个完整的自定义配置节示例代码:

 

 



  
    

    

  
  
  
    
    
  

 

程序中读取配置代码如下:

 

        static void Main(string[] args)
         {
            //访问 UrlString 配置节
             IDictionary dict = ConfigurationManager.GetSection("UrlString") as IDictionary;
             Console.WriteLine(string.Format("{0}?{1}", dict["action"], dict["paramString"]));

            //访问 UrlString2 配置节
             IDictionary dict2 = ConfigurationManager.GetSection("UrlString2") as IDictionary;
            foreach (DictionaryEntry e in dict2)
             {
                 Console.WriteLine(string.Format("{0}?{1}", e.Key, e.Value));
             }

             Console.Read();
         }

 

 

 

2. 自定义配置节组

配置节组是使用元素来声明,在配置节组中可以包括多个配置节

,如下:

 



    
        
            

        
    
    
    
        
            
        

    

 

下面是访问这个配置节组的代码:

 

NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]);    //输出Hello World

 

 

 

 

3. 通过 IConfigurationSectionHandler 接口实现自定义配置处理类

IConfigurationSectionHandler 接口原型:

 

public interface IConfigurationSectionHandler
{
     Object Create(Object parent, Object configContext, XmlNode section)
}

 

Create 方法必须可由多个线程同时调用,即要保证线程安全。

 

示例代码:

 

    public class UrlString : IConfigurationSectionHandler
     {

        private string _action;

        public string Action
         {
            get { return _action; }
            set { _action = value; }
         }

        private string _param;

        public string Param
         {
            get { return _param; }
            set { _param = value; }
         }

        #region IConfigurationSectionHandler 成员

        public object Create(object parent, object configContext, XmlNode section)
         {
             Hashtable hashtable = new Hashtable();
            foreach (XmlAttribute attribute in section.Attributes)
             {
                 hashtable[attribute.Name] = attribute.Value;
             }
            return hashtable;
         }

        #endregion

        public static UrlString Create()
         {
             UrlString us = new UrlString();
             Hashtable ht = ConfigurationManager.GetSection("UrlString") as Hashtable;
             us.Action = (string)ht["action"];
             us.Param = (string)ht["paramString"];
            return us;
         }
     }

转载于:https://www.cnblogs.com/doosmile/archive/2012/12/11/2812781.html

你可能感兴趣的:(web.config C#中使用自定义配置)