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

有经验的开发人员都知道在开发.NET应用时可以利用配置文件保存一些常用并且有可能变化的信息,例如日志文件的保存路径、数据库连接信息等等,这样即使生产环境中的参数信息与开发环境不一致也只需要更改配置文件而不用改动源代码再重新编译,极其方便。并且我们一般还约定,在<appSettings>节点保存应用程序的配置信息,在<connectionStrings>中保存数据库连接字符串信息(详见本博客《asp.net夜话之十一:web.config详解》)。
上面的这些方法和约定足以让我们在大部分开发中获得方便,但是在有些情况下有些配置信息可以按组分类存放,如果采用上面的方法不仅不直观,而且读取起来也不是太方便,幸好在.NET里就提供了这样的方法。如果有使用过Log4Net或者Enyim.Caching的朋友,肯定对下面的配置不会陌生:

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

或:

<configSections><section name="log4net" type="System.Configuration.IgnoreSectionHandler"/></configSections> 
<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> 
<?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;  
 
//作者:zhoufoxcn(周公)  
//日期:2011-03-08  
//blog:http://blog.csdn.net/zhoufoxcnhttp://zhoufoxcn.blog.51cto.com  
//版权声明:本文允许非商业用途,但必须保证不得去掉本文中的任何链接,违者必究。  
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; }  
        }  
 
    }  

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

//作者:zhoufoxcn(周公)
//日期:2011-03-08
//blog:http://blog.csdn.net/zhoufoxcnhttp://zhoufoxcn.blog.51cto.com
//版权声明:本文允许非商业用途,但必须保证不得去掉本文中的任何链接,违者必究。
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();  
        }  
    } 
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>也达到了类似的效果,但是通过自定义节点我们可以方便地读取相关的应用程序配置,同时也便于维护。如果在开发过程中遇到本文中类似的情况,不妨采取本文所述的方式。本文示例源代码可以到这里下载。

周公
2011-03-08

 

本文来自CSDN博客, 出处:http://blog.csdn.net/zhoufoxcn/archive/2011/03/09/6233171.aspx

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值