天天看點

.Net FrameWork2.0 以後的配置技術(System.Configuration)

應為編碼需要,要在配置檔案中增加如下的内容

 需求

 <configuration>

<configSections>

<sectionGroup name="GenerateHtmlConfig">

<section name="GenerateHtmlConfigSection" type="GenerateHtmlLib.Config.SectionHandler,GenerateHtmlLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

</sectionGroup>

</configSections>

<GenerateHtmlConfig>

<GenerateHtmlConfigSection isAvailable="true" TimeSpan="3">

<myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy"/>

<myChildConfigElementList>

<clear />

<add myChildAttrib1="Zippy" myChildAttrib2="1"/>

<add myChildAttrib1="222" myChildAttrib2="2"/>

</myChildConfigElementList>

</GenerateHtmlConfigSection>

</GenerateHtmlConfig>

...

</configuration>

這裡面用到了常用的節點,節點屬性,子元素,子元素集合

自定義節點的編碼需要繼承ConfigurationSection類

自定義子元素需要繼承configurationelement

自定義子元素集合需要繼承configurationelementcollection

節點屬性的例子在ConfigurationSection類的msdn中有

代碼

配置資訊自定義

using System;

using System.Collections;

using System.Configuration;

using System.Xml;

namespace GenerateHtmlLib.Config

{

public class SectionHandler : ConfigurationSection

{

/// <summary>

/// 本配置是否可用

/// </summary>

[ConfigurationProperty("isAvailable", DefaultValue = true, IsRequired = false)]

public Boolean IsAvailable

{

get { return (Boolean)this["isAvailable"]; }

set { this["isAvailable"] = value; }

}

/// <summary>

/// 間隔時間

/// </summary>

[ConfigurationProperty("TimeSpan", DefaultValue = 5, IsRequired = false)]

[IntegerValidator(MinValue = 1)]//排除負數

public int TimeSpan

{

get { return (int)this["TimeSpan"]; }

}

/// <summary>

/// 子元素,預設轉換規則

/// </summary>

[ConfigurationProperty("myChildSection")]

public MyChildConfigElement MyChildSection

{

get

{ return (MyChildConfigElement)this["myChildSection"]; }

set

{ this["myChildSection"] = value; }

}

/// <summary>

/// 子元素集合,轉換規則的集合

/// </summary>

[ConfigurationProperty("myChildConfigElementList")]

public MyChildConfigElementList MyChildSectionList

{

get

{

return this["myChildConfigElementList"] as MyChildConfigElementList;

}

set

{

this["myChildConfigElementList"] = value;

}

}

public class MyChildConfigElement : ConfigurationElement

{

public MyChildConfigElement()

{

}

public MyChildConfigElement(String a1, String a2)

{

MyChildAttribute1 = a1;

MyChildAttribute2 = a2;

}

[ConfigurationProperty("myChildAttrib1", DefaultValue = "Zippy", IsRequired = true)]

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'/"|//", MinLength = 1, MaxLength = 60)]

public String MyChildAttribute1

{

get

{ return (String)this["myChildAttrib1"]; }

set

{ this["myChildAttrib1"] = value; }

}

[ConfigurationProperty("myChildAttrib2", DefaultValue = "Michael Zawondy", IsRequired = true)]

[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'/"|//", MinLength = 1, MaxLength = 60)]

public String MyChildAttribute2

{

get

{ return (String)this["myChildAttrib2"]; }

set

{ this["myChildAttrib2"] = value; }

}

}

public sealed class MyChildConfigElementList : ConfigurationElementCollection

{

public MyChildConfigElement this[int index]

{

get

{

return base.BaseGet(index) as MyChildConfigElement;

}

set

{

if (index < base.Count)

{

if (base.BaseGet(index) != null)

{

base.BaseRemoveAt(index);

}

this.BaseAdd(index, value);

}

}

}

protected override ConfigurationElement CreateNewElement()

{

return new MyChildConfigElement();

}

protected override object GetElementKey(ConfigurationElement element)

{

return ((MyChildConfigElement)element).MyChildAttribute1;

}

}

}

}

配置資訊讀取

 <%@ Page Language="C#" %>

<mce:script runat="server"><!--

protected void Button1_Click(object sender, EventArgs e)

{

GenerateHtmlLib.Config.SectionHandler config =

(GenerateHtmlLib.Config.SectionHandler)System.Configuration.ConfigurationManager.GetSection(

"GenerateHtmlConfig/GenerateHtmlConfigSection");

StringBuilder sb = new StringBuilder();

sb.Append("<h2>Attributes in the myCustomSection Element:</h2>");

sb.AppendFormat("IsAvailable = {0}<br/>", config.IsAvailable.ToString());

sb.AppendFormat("TimeSpan = {0}<br/>", config.TimeSpan.ToString());

sb.Append("<h2>Attributes in the myChildSection Element:</h2>");

sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString());

sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString());

sb.Append("<h2>Attributes in the myChildSectionList Element:</h2>");

sb.AppendFormat(".MyChildSectionList[0].MyChildAttribute1 = {0}<br/>", config.MyChildSectionList[0].MyChildAttribute1.ToString());

sb.AppendFormat(".MyChildSectionList[1].MyChildAttribute2 = {0}<br/>", config.MyChildSectionList[1].MyChildAttribute1.ToString());

sb.AppendFormat(".MyChildSectionList.Count = {0}<br/>", config.MyChildSectionList.Count.ToString());

sb.AppendFormat(".MyChildSectionList.CollectionType = {0}<br/>", config.MyChildSectionList.CollectionType.ToString());

Label1.Text = sb.ToString();

Label1.Visible = true;

}

// --></mce:script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h1>Enumerate MyCustomSection</h1>

<asp:Label ID="Label1" runat="server"

Text="" />

<br />

<asp:Button ID="Button1" runat="server"

Text="Get Custom Config Info"

OnClick="Button1_Click" />

</div>

</form>

</body>

</html>

參考

http://hi.baidu.com/liupras/blog/item/e2ccbf50a1aef7658435240c.html

msndn的代碼示例要按下面的順序閱讀和複制執行:

http://msdn.microsoft.com/zh-cn/library/2tw134k3(v=VS.90).aspx

http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationelement(v=VS.90).aspx

http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationelementcollection(v=VS.90).aspx

繼續閱讀