在web.config和app.config文件中增加自定义配置节点的方法

 更新时间:2020年6月25日 11:38  点击:1665

有经验的开发人员都知道在开发.NET应用时可以利用配置文件保存一些常用并且有可能变化的信息,例如日志文件的保存路径、数据库连接信息等等,这样即使生产环境中的参数信息与开发环境不一致也只需要更改配置文件而不用改动源代码再重新编译,极其方便。并且我们一般还约定,在<appSettings>节点保存应用程序的配置信息,在<connectionStrings>中保存数据库连接字符串信息。

上面的这些方法和约定足以让我们在大部分开发中获得方便,但是在有些情况下有些配置信息可以按组分类存放,如果采用上面的方法不仅不直观,而且读取起来也不是太方便,幸好在.NET里就提供了这样的方法。如果有使用过Log4Net或者Enyim.Caching的朋友,肯定对下面的配置不会陌生:

复制代码 代码如下:

<sectionGroup name="enyim.com"><section name="memcached"
type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /></sectionGroup>

或:
复制代码 代码如下:

<configSections><section name="log4net" type="System.Configuration.IgnoreSectionHandler"/></configSections>

在出现上面配置的配置文件中,我们就会找到名称为"enyim.com"或者"log4net"的节点,尽管它们本不属于config文件的默认节点,但是通过上面的配置之后程序运行并不会报错。这样一来,相关配置信息也可以很好分类保存起来。

在这里我演示一个简单的例子,这个例子来源于我的一个从2006年起就开始开发的自用软件(因为没有美化所以没有免费发布),在这个应用程序的connfig文件中我增加了一些特有的配置,所以新增了一个自己的节点,app.config文件内容如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>  
   <configSections>      
    <section name="SoftwareSettings" type="ImageAssistant.Configuration.SoftwareSettings, ImageAssistant" />  
   </configSections>  
     <SoftwareSettings>    
       <LoadSettings>      
         <add key="LoadBmp" value="true"/>      
         <add key="LoadJpg" value="true"/>      
         <add key="LoadGif" value="true"/>      
         <add key="LoadPng" value="false"/>    
       </LoadSettings>    
     <PathSettings SavePath="C:\ResizeImages\" SearchSubPath="true"/>  
   </SoftwareSettings>  

   <appSettings>    
      <add key="LoadBmp" value="true"/>    
      <add key="LoadJpg" value="true"/>    
      <add key="LoadGif" value="true"/>    
      <add key="LoadPng" value="false"/>   
      <add key="IncludeSubPath"  value="true"/> 
   </appSettings>    
</configuration>


在config文件中我们使用<section name="SoftwareSettings" type="ImageAssistant.Configuration.SoftwareSettings, ImageAssistant" />告诉应用程序对于配置文件中的SoftwareSettings节点,其对应的类是ImageAssistant程序集中ImageAssistant.Configuration.SoftwareSettings类,并且在<SoftwareSettings>节点中我们还看到有<LoadSettings>节点和<PathSettings>节点,其中<LoadSettings>是一个节点集合,还包含有多个子节点,为了表示清楚这些关系我们需要添加四个类:SoftwareSettings、LoadSettingsCollection、LoadSettingsElement及PathSettingElement。为了发布方便,我将这四个类的代码放在一个物理文件中,代码如下(注意添加对System.Configuration.dll的引用):
复制代码 代码如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Configuration; 

namespace ImageAssistant.Configuration 

    public sealed class LoadSettingsCollection : ConfigurationElementCollection 
    { 
        private IDictionary<string, bool> settings; 

        protected override ConfigurationElement CreateNewElement() 
        { 
            return new LoadSettingsElement(); 
        } 

        protected override object GetElementKey(ConfigurationElement element) 
        { 
            LoadSettingsElement ep = (LoadSettingsElement)element; 

            return ep.Key; 
        } 

        protected override string ElementName 
        { 
            get 
            { 
                return base.ElementName; 
            } 
        } 

        public IDictionary<string, bool> Settings 
        { 
            get 
            { 
                if (settings == null) 
                { 
                    settings = new Dictionary<string, bool>(); 
                    foreach (LoadSettingsElement e in this) 
                    { 
                        settings.Add(e.Key, e.Value); 
                    } 
                } 
                return settings; 
            } 
        } 

        public bool this[string key] 
        { 
            get 
            { 
                bool isLoad = true; 
                if (settings.TryGetValue(key, out isLoad)) 
                { 
                    return isLoad; 
                } 
                else 
                { 
                    throw new ArgumentException("没有对'" + key + "'节点进行配置。"); 
                } 
            } 
        } 

    } 

    public class LoadSettingsElement : ConfigurationElement 
    { 
        [ConfigurationProperty("key", IsRequired = true)] 
        public string Key 
        { 
            get { return (string)base["key"]; } 
            set { base["key"] = value; } 
        } 
        [ConfigurationProperty("value", IsRequired = true)] 
        public bool Value 
        { 
            get { return (bool)base["value"]; } 
            set { base["value"] = value; } 
        } 
    } 

    public class PathSettingElement : ConfigurationElement 
    { 
        /// <summary>  
        ///   
        /// </summary>  
        [ConfigurationProperty("SavePath", IsRequired = true)] 
        public string SavePath 
        { 
            get { return (string)base["SavePath"]; } 
            set { base["SavePath"] = value; } 
        } 
        /// <summary>  
        ///   
        /// </summary>  
        [ConfigurationProperty("SearchSubPath", IsRequired = false, DefaultValue = true)] 
        public bool SearchSubPath 
        { 
            get { return (bool)base["SearchSubPath"]; } 
            set { base["SearchSubPath"] = value; } 
        } 
    } 

    /// <summary>  
    /// 对应config文件中的  
    /// </summary>  
    public sealed class SoftwareSettings : ConfigurationSection 
    { 
        /// <summary>  
        /// 对应SoftwareSettings节点下的LoadSettings子节点  
        /// </summary>  
        [ConfigurationProperty("LoadSettings", IsRequired = true)] 
        public LoadSettingsCollection LoadSettings 
        { 
            get { return (LoadSettingsCollection)base["LoadSettings"]; } 
        } 

        /// <summary>  
        /// 对应SoftwareSettings节点下的PathSettings子节点,非必须  
        /// </summary>  
        [ConfigurationProperty("PathSettings", IsRequired = false)] 
        public PathSettingElement PathSetting 
        { 
            get { return (PathSettingElement)base["PathSettings"]; } 
            set { base["PathSettings"] = value; } 
        } 

    } 
}

在上面的代码中可以看到ConfigurationProperty这个属性,这是表示对应的属性在config文件中的属性名,IsRequired表示是否是必须的属性,还有DefaultValue表示属性的默认值。初次之外,我们还要注意以下关系:
SoftwareSettings:根节点,继承自ConfigurationSection。
LoadSettingsCollection:子节点集合,继承自ConfigurationElementCollection。
LoadSettingsElement:子节点,继承自ConfigurationElement。
PathSettingElement:子节点,继承自ConfigurationElement。
编写了如下代码之后,我们又该如何使用上面的类呢?其实很简单,如下:
复制代码 代码如下:

class Program 
    { 
        static void Main(string[] args) 
        { 
            SoftwareSettings softSettings = ConfigurationManager.GetSection("SoftwareSettings") as SoftwareSettings; 

            foreach (string key in softSettings.LoadSettings.Settings.Keys) 
            { 
                Console.WriteLine("{0}={1}", key, softSettings.LoadSettings[key]); 
            } 
            Console.WriteLine("SavePath={0},SearchSubPath={1}", softSettings.PathSetting.SavePath, softSettings.PathSetting.SearchSubPath); 
            Console.ReadLine(); 
        } 
    }

这个程序的运行结果如下:
LoadBmp=True
LoadJpg=True
LoadGif=True
LoadPng=False
SavePath=C:/ResizeImages/,SearchSubPath=True

总结:在上面的config文件中通过<appSettings>也达到了类似的效果,但是通过自定义节点我们可以方便地读取相关的应用程序配置,同时也便于维护。如果在开发过程中遇到本文中类似的情况,不妨采取本文所述的方式。

[!--infotagslink--]

相关文章