c# 配置文件之configSections配置
对于小型项目来说,配置信息可以通过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="Data Source=.\sqlexpress;Initial Catalog=background;Integrated Security=True;MultipleActiveResultSets=True"" 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配置的更多相关文章
- c# 配置文件之configSections配置(二)
在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...
- c# 配置文件之configSections配置(三)
使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 using System; using System.Collections.G ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- C#开发中使用配置文件对象简化配置的本地保存
C#开发中使用配置文件对象简化配置的本地保存 0x00 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...
- WebConfig 自定义节点configSections配置信息
WebConfig 自定义节点configSections配置信息 示例: <configuration> <configSections> <!-- For ...
- (转)struts2.0配置文件、常量配置详解
一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...
- 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
- 通过Web.config中的configSections配置自己系统的全局常量
通过Web.config中的configSections配置自己系统的全局常量 随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧 首先在Web.C ...
- Windows Redis默认配置文件,Redis配置不生效解决方案
Windows Redis默认配置文件,Redis配置不生效解决方案, Windows Redis自启动配置不生效解决方案,Windows Redis增加自动启动服务 >>>> ...
随机推荐
- mysql 并发控制
1.多个线程同时修改数据,存在数据不一致的情况,也就是并发控制的问题.2.mysql提供读锁和写锁,读锁之上可以再加读锁,不能加写锁,而写锁之上不能加任何锁.也就是说,读锁是共享的,写锁是排他的.3. ...
- jquery_dialog实现效果
jquery_dialog实现效果 jquery_dialog.js <!-- /******************************************************** ...
- OpenGL中各种坐标系的理解[转]
OPENGL坐标系可分为:世界坐标系和当前绘图坐标系. 世界坐标系:在OpenGL中,世界坐标系是以屏幕中心为原点(0, 0, 0),且是始终不变的.你面对 屏幕,你的右边是x正轴,上面是y正轴,屏幕 ...
- 图解javascript中this指向
JavaScript 是一种脚本语言,支持函数式编程.闭包.基于原型的继承等高级功能.JavaScript一开始看起来感觉会很容易入门,但是随着使用的深入,你会发JavaScript其实很难掌握,有些 ...
- PacBio & BioNano (Assembly and diploid architecture of an individual human genome via single-molecule technologies)
Assembly and diploid architecture of an individual human genome via single-molecule technologies 文章链 ...
- tif图片编辑利器
http://www.onlinedown.net/soft/99112.htmTIF编辑器 0.4 http://www.zjda07.cn/软件类别:国产软件/图像处理软件大小:1089KB软件授 ...
- centos7 安装mariaDB 以及 phpmyadmin的安装
centos7 安装mariaDB 以及 phpmyadmin的安装 一:安装mariadb, mariadb 是 mysql 的一个分支,基本和mysql一样的 1. yum -y install ...
- Java Annotation 及几个常用开源项目注解原理简析
PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...
- Win7下Maven的安装与配置
简介 官网:https://maven.apache.org/ Apache Maven,是一个(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供.基于项目对象模型(Pro ...
- Xcode 8 打包上线 iTunes Connect 找不到构建版本
Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...