zoukankan      html  css  js  c++  java
  • 在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>  

    或:

    <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的引用):

     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Configuration;  
    6.   
    7. //作者:zhoufoxcn(周公)  
    8. //日期:2011-03-08  
    9. //blog:http://blog.csdn.net/zhoufoxcn 或http://zhoufoxcn.blog.51cto.com  
    10. //版权声明:本文允许非商业用途,但必须保证不得去掉本文中的任何链接,违者必究。  
    11. namespace ImageAssistant.Configuration  
    12. {  
    13.     public sealed class LoadSettingsCollection : ConfigurationElementCollection  
    14.     {  
    15.         private IDictionary<stringbool> settings;  
    16.   
    17.         protected override ConfigurationElement CreateNewElement()  
    18.         {  
    19.             return new LoadSettingsElement();  
    20.         }  
    21.   
    22.         protected override object GetElementKey(ConfigurationElement element)  
    23.         {  
    24.             LoadSettingsElement ep = (LoadSettingsElement)element;  
    25.   
    26.             return ep.Key;  
    27.         }  
    28.   
    29.         protected override string ElementName  
    30.         {  
    31.             get  
    32.             {  
    33.                 return base.ElementName;  
    34.             }  
    35.         }  
    36.   
    37.         public IDictionary<stringbool> Settings  
    38.         {  
    39.             get  
    40.             {  
    41.                 if (settings == null)  
    42.                 {  
    43.                     settings = new Dictionary<stringbool>();  
    44.                     foreach (LoadSettingsElement e in this)  
    45.                     {  
    46.                         settings.Add(e.Key, e.Value);  
    47.                     }  
    48.                 }  
    49.                 return settings;  
    50.             }  
    51.         }  
    52.   
    53.         public bool this[string key]  
    54.         {  
    55.             get  
    56.             {  
    57.                 bool isLoad = true;  
    58.                 if (settings.TryGetValue(key, out isLoad))  
    59.                 {  
    60.                     return isLoad;  
    61.                 }  
    62.                 else  
    63.                 {  
    64.                     throw new ArgumentException("没有对'" + key + "'节点进行配置。");  
    65.                 }  
    66.             }  
    67.         }  
    68.   
    69.     }  
    70.   
    71.     public class LoadSettingsElement : ConfigurationElement  
    72.     {  
    73.         [ConfigurationProperty("key", IsRequired = true)]  
    74.         public string Key  
    75.         {  
    76.             get { return (string)base["key"]; }  
    77.             set { base["key"] = value; }  
    78.         }  
    79.         [ConfigurationProperty("value", IsRequired = true)]  
    80.         public bool Value  
    81.         {  
    82.             get { return (bool)base["value"]; }  
    83.             set { base["value"] = value; }  
    84.         }  
    85.     }  
    86.   
    87.     public class PathSettingElement : ConfigurationElement  
    88.     {  
    89.         /// <summary>  
    90.         ///   
    91.         /// </summary>  
    92.         [ConfigurationProperty("SavePath", IsRequired = true)]  
    93.         public string SavePath  
    94.         {  
    95.             get { return (string)base["SavePath"]; }  
    96.             set { base["SavePath"] = value; }  
    97.         }  
    98.         /// <summary>  
    99.         ///   
    100.         /// </summary>  
    101.         [ConfigurationProperty("SearchSubPath", IsRequired = false, DefaultValue = true)]  
    102.         public bool SearchSubPath  
    103.         {  
    104.             get { return (bool)base["SearchSubPath"]; }  
    105.             set { base["SearchSubPath"] = value; }  
    106.         }  
    107.     }  
    108.   
    109.     /// <summary>  
    110.     /// 对应config文件中的  
    111.     /// </summary>  
    112.     public sealed class SoftwareSettings : ConfigurationSection  
    113.     {  
    114.         /// <summary>  
    115.         /// 对应SoftwareSettings节点下的LoadSettings子节点  
    116.         /// </summary>  
    117.         [ConfigurationProperty("LoadSettings", IsRequired = true)]  
    118.         public LoadSettingsCollection LoadSettings  
    119.         {  
    120.             get { return (LoadSettingsCollection)base["LoadSettings"]; }  
    121.         }  
    122.   
    123.         /// <summary>  
    124.         /// 对应SoftwareSettings节点下的PathSettings子节点,非必须  
    125.         /// </summary>  
    126.         [ConfigurationProperty("PathSettings", IsRequired = false)]  
    127.         public PathSettingElement PathSetting  
    128.         {  
    129.             get { return (PathSettingElement)base["PathSettings"]; }  
    130.             set { base["PathSettings"] = value; }  
    131.         }  
    132.   
    133.     }  
    134. }  

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

    1. class Program  
    2.     {  
    3.         static void Main(string[] args)  
    4.         {  
    5.             SoftwareSettings softSettings = ConfigurationManager.GetSection("SoftwareSettings"as SoftwareSettings;  
    6.   
    7.             foreach (string key in softSettings.LoadSettings.Settings.Keys)  
    8.             {  
    9.                 Console.WriteLine("{0}={1}", key, softSettings.LoadSettings[key]);  
    10.             }  
    11.             Console.WriteLine("SavePath={0},SearchSubPath={1}", softSettings.PathSetting.SavePath, softSettings.PathSetting.SearchSubPath);  
    12.             Console.ReadLine();  
    13.         }  
    14.     }  

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

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

    周公 2011-03-08

  • 相关阅读:
    angular-ui-bootstrap插件API
    简易富文本编辑器bootstrap-wysiwyg源码注释
    deployd使用归纳
    初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助
    COCOS2D-JS入门-官网template源码解析
    COCOS2D-JS入门-web端项目部署
    jQuery 插件格式
    react native 使用 native-echarts 在安卓上无法显示解决办法
    springboot项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde
    Mac 上 Class JavaLaunchHelper is implemented in both 报错
  • 原文地址:https://www.cnblogs.com/xxaxx/p/3050183.html
Copyright © 2011-2022 走看看