对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案

1 自己开发一个配置信息持久化类,用来管理配置信息,并提供面向对象的支持
2 使用.net自带的configSections,将配置信息分块管理,并提供实体类,便于开发人员友好的去使用它

本文主要说说第二种方案,它由实体类,实体类工厂及配置文件三个部分,看代码:

实体类设计:

 namespace Configer
{
/// <summary>
/// 网站信息配置节点
/// </summary>
public class WebConfigSection : ConfigurationSection
{
/// <summary>
/// 网站名称
/// </summary>
[ConfigurationProperty("WebName", DefaultValue = "", IsRequired = true, IsKey = false)]
public string WebName
{ get { return (string)this["WebName"]; }
set { this["WebName"] = value; }
}
/// <summary>
/// 网站域名
/// </summary>
[ConfigurationProperty("DoMain", DefaultValue = "", IsRequired = true, IsKey = false)]
public string DoMain
{ get { return (string)this["DoMain"]; }
set { this["DoMain"] = value; }
} }
}

实体工厂类设计,主要用来生产实体配置信息

 namespace Configer
{
/// <summary>
/// 网站配置信息工厂
/// </summary>
public class WebConfigManager
{
/// <summary>
/// 配置信息实体
/// </summary>
public static readonly WebConfigSection Instance = GetSection(); private static WebConfigSection GetSection()
{
WebConfigSection config = ConfigurationManager.GetSection("WebConfigSection") as WebConfigSection;
if (config == null)
throw new ConfigurationErrorsException();
return config;
}
}
}

而最后就是.config文件了,它有configSections和指定的sections块组成,需要注意的是configSections必须位于configuration的第一个位置

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="WebConfigSection" type="Configer.WebConfigSection, test"/>
</configSections>
<connectionStrings>
<add name="backgroundEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\sqlexpress;Initial Catalog=background;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings> <WebConfigSection WebName="占占网站" DoMain="www.zhanzhan.com" />
<appSettings>
<add key="site" value="www.zzl.com"/> </appSettings>
</configuration>

以上三步实现后,我们就可以调用了,呵呵

   static void Main(string[] args)
{
Console.WriteLine(System.Configuration.ConfigurationManager.AppSettings["site"]);
Console.WriteLine(WebConfigManager.Instance.DoMain);
Console.WriteLine(WebConfigManager.Instance.WebName);
}

c# 配置文件之configSections配置的更多相关文章

  1. c# 配置文件之configSections配置(二)

    在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...

  2. c# 配置文件之configSections配置(三)

    使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...

  3. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  4. C#开发中使用配置文件对象简化配置的本地保存

    C#开发中使用配置文件对象简化配置的本地保存 0x00 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...

  5. WebConfig 自定义节点configSections配置信息

    WebConfig 自定义节点configSections配置信息 示例: <configuration>   <configSections>     <!-- For ...

  6. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  7. 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    [转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...

  8. 通过Web.config中的configSections配置自己系统的全局常量

    通过Web.config中的configSections配置自己系统的全局常量 随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧 首先在Web.C ...

  9. Windows Redis默认配置文件,Redis配置不生效解决方案

    Windows Redis默认配置文件,Redis配置不生效解决方案, Windows Redis自启动配置不生效解决方案,Windows Redis增加自动启动服务 >>>> ...

随机推荐

  1. Python学习(2)基本语法

    目录 交互式编程 脚本式编程 Python 标识符 Python保留字符 行和缩进 多行语句 Python 引号 Python注释 Python空行 python的输入和输出 命令行参数 交互式编程 ...

  2. Ubuntu Server14.04 32位安装odoo8.0简单方法

    一.wget -O - https://nightly.odoo.com/odoo.key | apt-key add - 二.echo "deb http://nightly.odoo.c ...

  3. 使用fragment兼容低版本的写法

      [1]定义fragment继承V4包中的Fragment    [2]定义的activity要继承v4包中的FragmentActivity   [3]通过这个方法getSupportFragme ...

  4. pod JONSKit.h MBProgress.h 找不到头文件,怎么办?

    这时你看项目pod部分,多了JSONKit库.好了,第三方库就这么神奇的加进来. 头文件路径 那试试看使用JONSKit.h,在ViewController.m里引用下.找不到头文件,怎么办?还没设置 ...

  5. python的变量传递

    python中变量都被视为对象的引用.python函数调用传递参数的时候,不允许程序员选择传值还是传引用,python参数传递采用的都是“传对象引用”的方式.     这种方式相当于传值和传引用的结合 ...

  6. 如何部署Scrapy 到Scrapyd上?

    安装上传工具 1.上传工具 scrapyd-client 2.安装方法: pip install scrapyd-client 3.上传方法: python d:\Python27\Scripts\s ...

  7. js中正则表达式 书写方法

    function test(){    var text="index.aspx?test=1&ww=2&www=3";            var   re = ...

  8. ajax的详细学习

    地址:http://www.cnblogs.com/Garden-blog/archive/2011/03/11/1981778.html

  9. H5网页播放器播不了服务器上的mp4视频文件

    打开IIS,在功能视图里找到MIME类型菜单,打开该菜单后鼠标右键添加.mp4扩展名的MIME类型video/mp4 其他视频文件播放不了估计也得在IIS里添加对应的MIME类型(从服务器下载文件时也 ...

  10. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...