在我们实际的项目开发过程中有很多地方要用到配置文件.虽然.NET提供了内置的几种配置文件,如app.config,web.config.但总有很多时候这些配置文件不能满足我们的需求,这时候就需要打造我们自己的配置文件了.

要打造我们自己的配置文件有很多种方法,如自定义格式文本,XML文件等.这里我仅说明如何利用.NET提供的配置文件架构来自定义配置文件,以此达到”我的配置文件我做主”.

.NET为我们提供了灵活,全面,使用简单的配置文件基础架构,如果要实现自己的配置文件,那么仅需要扩展下面几个类即可.

System.Configuration.ConfigurationSection

System.Configuration.ConfigurationElement

System.Configuration.ConfigurationElementCollection

如果要知道这几个类的详细信息,请查阅MSDN: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/fxref/html/41d62742-7669-44ea-787b-a8ab7c0ae8da.htm,这里面是最好的解释.

下面以我做的一个GIS项目为例,一步一步来打造自己的配置文件.

  • 打造配置元素

配置元素即我们实际存放配置的地方,这个要用ConfigurationElement继承扩展而来.实现如下:

 

代码
namespace Niyw.GIS.Map {
public class CellElement : ConfigurationElement {
#region Fields
protected static ConfigurationPropertyCollection _Cells;
private static ConfigurationProperty _Type;
private static ConfigurationProperty _Color;
private static ConfigurationProperty _Style;
#endregion
#region Properties
/// <summary>
/// 样式
/// </summary>
public string Style {
get { return (string)base[_Style]; }
set { base[_Style] = value; }
}
/// <summary>
/// 网络类型
/// </summary>
public string Type {
get { return (string)base[_Type]; }
set { base[_Type] = value; }
}
/// <summary>
/// 小区颜色
/// </summary>
public Color Color {
get { return (Color)base[_Color]; }
set { base[_Color] = value; }
}
protected override ConfigurationPropertyCollection Properties {
get {
return _Cells;
}
}
#endregion
#region Constructors
static CellElement() {
_Style
= new ConfigurationProperty(
"Style",
typeof(string),
"Pie",
ConfigurationPropertyOptions.IsRequired
);
_Type
= new ConfigurationProperty(
"Type",
typeof(string),
"GSM",
ConfigurationPropertyOptions.IsRequired
);
_Color
= new ConfigurationProperty(
"Color",
typeof(Color),
Color.GreenYellow,
ConfigurationPropertyOptions.IsRequired
);

_Cells
= new ConfigurationPropertyCollection();

_Cells.Add(_Type);
_Cells.Add(_Color);
_Cells.Add(_Style);
}
#endregion
}
}

 这里要注意到,我们存放配置数据的项全部都是ConfigurationProperty类型,关于ConfigurationProperty的详细信息请参阅MSDN.

       相应的配置文件中的效果为:

<Cell Type="GSM" Color="GreenYellow" Style="Pie" />

  • 打造配置元素集合

配置元素集合虽然不是必须项,但通常是必要项,如最常用的appsettings.实现自己的配置元素集合需要从ConfigurationElementCollection继承而来,如下:

 

代码
namespace Niyw.GIS.Map {
public class CellElementCollection : ConfigurationElementCollection {
#region Properties
public override ConfigurationElementCollectionType CollectionType {
get {
return ConfigurationElementCollectionType.BasicMap;
}
}
/// <summary>
/// 不是必须重载.
/// 配置文件中默认的每一项都是以Add开头,重载该属性则可以灵活定制每种配置元素的头部
/// </summary>
protected override string ElementName {
get {
return "Cell";
}
}
protected override ConfigurationPropertyCollection Properties {
get {
return new ConfigurationPropertyCollection();
}
}
public CellElement this[int index] {
get {
return (CellElement)base.BaseGet(index);
}
set {
if (base.BaseGet(index) != null) {
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
new public CellElement this[string name] {
get {
return (CellElement)base.BaseGet(name);
}
}
#endregion
#region Constructor
public CellElementCollection() {
}
#endregion
#region Methods如果配置文件没有动态编辑的需要,则不用实现以下这些方法
public void Add(CellElement item) {
base.BaseAdd(item);
}

public void Remove(CellElement item) {
base.BaseRemove(item);
}

public void RemoveAt(int index) {
base.BaseRemoveAt(index);
}
#endregion
#region Overrides
protected override ConfigurationElement CreateNewElement() {
return new CellElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return (element as CellElement).Type;
}
#endregion
}
}

 此处需注意其中的注释。对应配置文件中节为

  <Cells>

     <Cell Type="GSM" Color="GreenYellow" Style="Pie" />

  • 打造配置节

配置节是配置文件中最重的部分,要实现自己的配置节,需从ConfigurationSection继承过来,代码如下:

 

代码
namespace Niyw.GIS.Map {
public class MapSection : ConfigurationSection {
#region Fields
private static ConfigurationPropertyCollection _Properties;
private static ConfigurationProperty _Cells;
private static ConfigurationProperty _Name;
#endregion
#region Properties
public string Name {
get { return (string)base[_Name]; }
set { base[_Name] = value; }
}
/// <summary>
/// 小区配置项
/// </summary>
public CellElementCollection Cells {
get { return (CellElementCollection)base[_Cells]; }
}
protected override ConfigurationPropertyCollection Properties {
get {
return _Properties;
}
}
#endregion
#region Constructors
static MapSection() {
_Cells
= new ConfigurationProperty("Cells", typeof(CellElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);
_Name
= new ConfigurationProperty("name", typeof(string), null, ConfigurationPropertyOptions.IsRequired);
_Properties
= new ConfigurationPropertyCollection();
_Properties.Add(_Cells);
_Properties.Add(_Name);
}
#endregion
}
}

 对应配置文件中的项为

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections >

    <section name="Map" type="Niyw.GIS.Map.MapSection, MyGIS" />

  </configSections>

  <Map name="Map">

    <Cells>

      <Cell Type="GSM" Color="GreenYellow" Style="Pie" />

这里需要注意自定义的配置文件必须在<configSections >节添加相应的说明信息,如<section name="Map" type="Niyw.GIS.Map.MapSection, MyGIS" />。其中name指定自定义配置节的名字,type指定了配置节类定义所在的程序集名称“MyGIS”和命名空间“Niyw.GIS.Map.MapSection”,二者缺一不可。

  • 管理配置文件

如果觉得配置文件中内容太多,那么我们可以将自己的配置项放到单独的文件中。要实现这一步,非常简单。我们只需要在app.config文件最后添加:  <Map configSource ="Config\Map.config"></Map>,然后就可以将自己的配置项放到Config目录下的Map.config文件中了。此处需要说明的时,上面的内容必须放在app.config文件最后,否则会报错。另外,我们注意到指定路径的configSource项我们并没有定义,这是因为configSource项是配置节的隐藏默认属性,不需要定义即可使用。

  • 程序中使用

实现代码: 

代码
private void ReadAndEdit()
{
MapSection mapset
= ConfigurationManager.GetSection("Cell"); //读取配置节
Color c = mapset.Cells[0].Color;//读取一项配置中的数据

//编辑配置节
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MapSection section
= config.GetSection("Cell") as MapSection;
ClutterElement ce
= section.Cells[0];//获取编辑项
if (ce != null){//修改数据
ce.Color = color;
ce.Name
= name;
}
config.Save();
//保存修改
ConfigurationManager.RefreshSection("Cell");//刷新配置节
}

这里需要注意保存修改后需要刷新配置节,否则读取不到修改结果.

 

好了,以上是自定义配置文件的整个过程,简单吧.

posted on 2010-07-17 23:17  倪大虾  阅读(2840)  评论(9编辑  收藏  举报