对于小型项目来说,配置信息可以通过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. Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...

  2. Linux下利用rsync实现多服务器文件同步

    windows做为文件服务器,使用rsync的windows服务版本,然后配置好就可以了.需要的朋友可以参考下. windows做为文件服务器,使用rsync的windows服务版本:cwRsyncS ...

  3. J2EE 第二阶段项目之编写代码(六)

    三张表的增 修改 查看.明天可以完成. 周末继续统计.

  4. css-高度自适应的问题(body高度问题)

    css-高度自适应的问题 对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{ height:100%; } 上面的css样式是无效的,不会产生任何效果. 需要改写: ...

  5. MySQL OCP 考试,一个不错的网站

    http://www.aiotestking.com/oracle/category/exam-1z0-883-mysql-5-6-database-administrator/page/10/ 里面 ...

  6. Windows环境配置Apache+Mysql+PHP

    一.安装配置Apache2.4.7(httpd-2.4.7-win64-VC11.zip ) 1.解压下载的安装包:httpd-2.4.7-win64-VC11.zip将其放到自己的安装目录(我的目录 ...

  7. .NET Reflector 8.2支持VS2013高亮显示和代码地图视图

    Red Gate Software公司最近发布的.NET Reflector 8.2支持Visual Studio 2013,其Reflector 桌面程序能够转换十六进制/十进制值.桌面程序还支持局 ...

  8. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  9. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  10. ThinkPHP框架如何修改X-Powered-By

    以前用ThinkPHP框架开发了一个小网站,前几天查询页面HTTP状态发现,里面有一项: X-Powered-By: ThinkPHP 2.0 这样虽然没什么,但感觉如果别有用心的人查询会知道你是用这 ...