c# 配置文件之configSections配置

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

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

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

实体类设计:

 1 namespace Configer
 2 {
 3     /// <summary>
 4     /// 网站信息配置节点
 5     /// </summary>
 6     public class WebConfigSection : ConfigurationSection
 7     {
 8         /// <summary>
 9         /// 网站名称
10         /// </summary>
11         [ConfigurationProperty("WebName", DefaultValue = "", IsRequired = true, IsKey = false)]
12         public string WebName
13         {
14
15             get { return (string)this["WebName"]; }
16             set { this["WebName"] = value; }
17         }
18         /// <summary>
19         /// 网站域名
20         /// </summary>
21         [ConfigurationProperty("DoMain", DefaultValue = "", IsRequired = true, IsKey = false)]
22         public string DoMain
23         {
24
25             get { return (string)this["DoMain"]; }
26             set { this["DoMain"] = value; }
27         }
28
29     }
30 }

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

 1 namespace Configer
 2 {
 3     /// <summary>
 4     /// 网站配置信息工厂
 5     /// </summary>
 6     public class WebConfigManager
 7     {
 8         /// <summary>
 9         /// 配置信息实体
10         /// </summary>
11         public static readonly WebConfigSection Instance = GetSection();
12
13         private static WebConfigSection GetSection()
14         {
15             WebConfigSection config = ConfigurationManager.GetSection("WebConfigSection") as WebConfigSection;
16             if (config == null)
17                 throw new ConfigurationErrorsException();
18             return config;
19         }
20     }
21 }

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <configSections>
 4     <section name="WebConfigSection" type="Configer.WebConfigSection, test"/>
 5   </configSections>
 6   <connectionStrings>
 7     <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" />
 8   </connectionStrings>
 9
10   <WebConfigSection WebName="占占网站" DoMain="www.zhanzhan.com"  />
11   <appSettings>
12     <add key="site" value="www.zzl.com"/>
13
14   </appSettings>
15 </configuration>

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

1   static void Main(string[] args)
2    {
3      Console.WriteLine(System.Configuration.ConfigurationManager.AppSettings["site"]);
4      Console.WriteLine(WebConfigManager.Instance.DoMain);
5      Console.WriteLine(WebConfigManager.Instance.WebName);
6    }
时间: 2024-08-27 19:29:00

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

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

在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System.Configuration.NameValueSectionHandler       System.Configuration.SingleTagSectionHandler DictionarySectionHandler使用 DictionarySectionHandler的工作方式与Name

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

使用IConfigurationSectionHandler配置configSections ·步骤1:定义一个实体类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 /// <summary> 10 /// 说明

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

前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文件中的配置貌似出现了问题.查找节点发现是如下节点: <configSections> <section name="Test1" type="Demo.Section1,Demo"/> .............. </configSect

读取配置文件异常,配置系统未能初始化

异常原因:配置文件内容的顺序有一定要求 configSections-->connectionStrings-->appSettings <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="WebConfigSection" type="MediaActionSe

MyBatis学习总结(三)——优化MyBatis配置文件中的配置(转载)

孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(三)--优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org

vsftpd配置文件详解 ---配置解说

vsftpd配置文件详解 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为ftp或anonymous,口令为空:匿名用户不能离开匿名用户家目录/var/ftp,且只能下载不能上传. 3>本地用户的登录名为本地用户名,口令为此本地用户的口令:本地用户可以在自 己家目录中进行读写操作:本地用户可以离开自家目录切换至有权限访问的其他目录,并在权限允许的情况下进行上传/下载. write_en

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

[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//E

Spring 配置文件中如何配置数据库连接

xml配置文件中配置如下:  <spring:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <spring:property name="locations"> <spring:list> <spring:value>classp

Flash builder发布Air程序时设备配置文件supportedProfiles的配置

1. 发布的程序:需要访问本地进程,那么只能发布为exe程序才可以.   此时supportedProfiles 配置为 extendedDesktop desktop   desktop保证能发布air时不会出错,而访问本地进程则需要extendedDesktop 2. 本地调试:需要访问本地进程   此时supportedProfiles 配置为 extendedDesktop.如果配置为desktop运行会报错,提示没有权限运行本地进程.   为了不至于在发布与调试的时候来回改suppor