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

时间:2021-11-03 03:21:41 

有经验的开发人员都知道在开发.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>也达到了类似的效果,但是通过自定义节点我们可以方便地读取相关的应用程序配置,同时也便于维护。如果在开发过程中遇到本文中类似的情况,不妨采取本文所述的方式。

标签:app.config,web.config
0
投稿

猜你喜欢

  • Spring Boot实现分布式系统中的服务发现和注册(最新推荐)

    2022-07-10 03:50:23
  • 浅析Java自定义注解的用法

    2022-06-08 01:52:58
  • 聊聊在Servlet中怎么上传文件

    2022-03-07 17:56:44
  • Java实现数据库连接池的方法

    2023-11-28 08:57:30
  • 深入C# 内存管理以及优化的方法详解

    2021-10-24 14:24:03
  • C#使用iTextSharp将PDF转成文本的方法

    2022-05-03 16:59:48
  • 基于spring data jpa @query返回map的踩坑记录

    2023-11-27 22:51:00
  • spring cloud gateway 限流的实现与原理

    2023-04-10 16:47:56
  • Android提高之MediaPlayer播放网络视频的实现方法

    2021-07-03 06:25:29
  • 通俗易懂的C#之反射教程

    2021-08-08 19:35:02
  • Java两种常用的随机数生成方式(小白总结)

    2023-02-16 16:54:19
  • Maven 配置文件 生命周期 常用命令详解

    2022-07-05 19:59:39
  • 【C#基础】Substring截取字符串的方法小结(推荐)

    2021-12-21 06:22:32
  • Android studio报: java.lang.ExceptionInInitializerError 错误

    2022-08-14 14:21:30
  • SpringMVC数据输出相关知识总结

    2022-04-06 10:35:06
  • 深入讲解spring boot中servlet的启动过程与原理

    2022-08-19 00:18:40
  • 举例讲解JDK注解的使用和自定义注解的方法

    2022-06-29 17:34:52
  • Java实现爬取百度图片的方法分析

    2023-12-19 23:51:27
  • Java8新特性之接口中的默认方法和静态方法

    2021-08-21 07:25:43
  • Web容器启动过程中如何执行Java类

    2022-10-01 19:49:10
  • asp之家 软件编程 m.aspxhome.com