App.config是.net自带的配置文件,对它的自定义方法一直也没有去看一下。今天在同事郑学剑的激励和网络红人贾君鹏同学的感召下简单学习了一下。现将学习的一点粗浅的心得记录于下。
自定义的包括自定义节的定义和具体配置两部分。
定义部分以<configSections>作根节点,并且此节点必须为App.config文件根节点下的第一个元素,其下可以包括<sectionGroup>、<section>两种节点,<sectionGroup>下可以包含<section>或子一级的<sectionGroup>。<sectionGroup>和<section>都需要包含一个name属性,这个属性将用于稍后的具体配置部分。
<section>节点还应包含一个type属性,type属性可以为一下三个值:
System.Configuration.NameValueSectionHandler
System.Configuration.DictionarySectionHandler
System.Configuration.SingleTagSectionHandler
具体配置部分的节点名为前面定义的<sectionGroup>和<section>节点的name属性值,结构和定义部分一致。按type属性值的不同<section>节点中可以以不同的格式添加具体的配置信息。
NameValueSectionHandler和DictionarySectionHandler添加的格式:
<section><add key="setting" value="value"></add>...</section>
SingleTagSectionHandler添加的格式:
<section setting="value" ... />
示例App.config
在代码中可以使用 System.Configuration.ConfigurationManager类中静态方法GetSection访问自定义的配置项,参数为具体Section的path,返回值为Object类型。但对不同的Section的类型(type)需要将结果转换为不同的类型。(要访问System.Configuration.ConfigurationManager类,需要在工程中添加System.Configuration的引用)
System.Configuration.NameValueSectionHandler——〉System.Collections.Specialized.NameValueCollection
System.Configuration.DictionarySectionHandler——〉System.Collections.Hashtable
System.Configuration.SingleTagSectionHandler—— 〉 System.Collections.IDictionary
代码示例
自定义的包括自定义节的定义和具体配置两部分。
定义部分以<configSections>作根节点,并且此节点必须为App.config文件根节点下的第一个元素,其下可以包括<sectionGroup>、<section>两种节点,<sectionGroup>下可以包含<section>或子一级的<sectionGroup>。<sectionGroup>和<section>都需要包含一个name属性,这个属性将用于稍后的具体配置部分。
<section>节点还应包含一个type属性,type属性可以为一下三个值:
System.Configuration.NameValueSectionHandler
System.Configuration.DictionarySectionHandler
System.Configuration.SingleTagSectionHandler
具体配置部分的节点名为前面定义的<sectionGroup>和<section>节点的name属性值,结构和定义部分一致。按type属性值的不同<section>节点中可以以不同的格式添加具体的配置信息。
NameValueSectionHandler和DictionarySectionHandler添加的格式:
<section><add key="setting" value="value"></add>...</section>
SingleTagSectionHandler添加的格式:
<section setting="value" ... />
示例App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="mySectionGroup">
<section name="mySection1" type="System.Configuration.NameValueSectionHandler"/>
<section name="mySection2" type="System.Configuration.DictionarySectionHandler"/>
<section name="mySection3" type="System.Configuration.SingleTagSectionHandler"/>
</sectionGroup>
</configSections>
<mySectionGroup>
<mySection1>
<add key="name" value="贾君鹏"></add>
<add key="sender" value="你妈妈叫你"></add>
<add key="action" value="回家吃饭"></add>
</mySection1>
<mySection2>
<add key="name" value="贾君鹏"></add>
<add key="sender" value="你妈妈叫你"></add>
<add key="action" value="回家吃饭"></add>
</mySection2>
<mySection3 name="贾君鹏" sender="你妈妈叫你" action="回家吃饭"/>
</mySectionGroup>
</configuration>
System.Configuration.NameValueSectionHandler——〉System.Collections.Specialized.NameValueCollection
System.Configuration.DictionarySectionHandler——〉System.Collections.Hashtable
System.Configuration.SingleTagSectionHandler—— 〉 System.Collections.IDictionary
代码示例
private void testButton_Click(object sender, EventArgs e)
{
NameValueCollection section1 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection1")
as System.Collections.Specialized.NameValueCollection;
string name1 = section1["name"];
string sender1 = section1["sender"];
string action1 = section1["action"];
Hashtable section2 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection2")
as System.Collections.Hashtable;
string name2 = section2["name"].ToString();
string sender2 = section2["sender"].ToString();
string action2 = section2["action"].ToString();
IDictionary section3 = System.Configuration.ConfigurationManager.GetSection("mySectionGroup/mySection3")
as System.Collections.IDictionary;
string name3 = section3["name"].ToString();
string sender3 = section3["sender"].ToString();
string action3 = section3["action"].ToString();
this.showTextBox.Text = name1 + sender1 + action1 + "\r\n"
+ name2 + sender2 + action2 + "\r\n"
+ name3 + sender3 + action3 + "\r\n";
}