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.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
1
<?
xml version="1.0" encoding="utf-8"
?>
2 < configuration >
3 < configSections >
4 < sectionGroup name ="mySectionGroup" >
5 < section name ="mySection1" type ="System.Configuration.NameValueSectionHandler" />
6 < section name ="mySection2" type ="System.Configuration.DictionarySectionHandler" />
7 < section name ="mySection3" type ="System.Configuration.SingleTagSectionHandler" />
8 </ sectionGroup >
9 </ configSections >
10
11 < mySectionGroup >
12 < mySection1 >
13 < add key ="name" value ="贾君鹏" ></ add >
14 < add key ="sender" value ="你妈妈叫你" ></ add >
15 < add key ="action" value ="回家吃饭" ></ add >
16 </ mySection1 >
17 < mySection2 >
18 < add key ="name" value ="贾君鹏" ></ add >
19 < add key ="sender" value ="你妈妈叫你" ></ add >
20 < add key ="action" value ="回家吃饭" ></ add >
21 </ mySection2 >
22 < mySection3 name ="贾君鹏" sender ="你妈妈叫你" action ="回家吃饭" />
23 </ mySectionGroup >
24 </ configuration >
在代码中可以使用 System.Configuration.ConfigurationManager类中静态方法GetSection访问自定义的配置项,参数为具体Section的path,返回值为Object类型。但对不同的Section的类型(type)需要将结果转换为不同的类型。(要访问System.Configuration.ConfigurationManager类,需要在工程中添加System.Configuration的引用)
2 < configuration >
3 < configSections >
4 < sectionGroup name ="mySectionGroup" >
5 < section name ="mySection1" type ="System.Configuration.NameValueSectionHandler" />
6 < section name ="mySection2" type ="System.Configuration.DictionarySectionHandler" />
7 < section name ="mySection3" type ="System.Configuration.SingleTagSectionHandler" />
8 </ sectionGroup >
9 </ configSections >
10
11 < mySectionGroup >
12 < mySection1 >
13 < add key ="name" value ="贾君鹏" ></ add >
14 < add key ="sender" value ="你妈妈叫你" ></ add >
15 < add key ="action" value ="回家吃饭" ></ add >
16 </ mySection1 >
17 < mySection2 >
18 < add key ="name" value ="贾君鹏" ></ add >
19 < add key ="sender" value ="你妈妈叫你" ></ add >
20 < add key ="action" value ="回家吃饭" ></ add >
21 </ mySection2 >
22 < mySection3 name ="贾君鹏" sender ="你妈妈叫你" action ="回家吃饭" />
23 </ mySectionGroup >
24 </ configuration >
System.Configuration.NameValueSectionHandler——〉System.Collections.Specialized.NameValueCollection
System.Configuration.DictionarySectionHandler——〉System.Collections.Hashtable
System.Configuration.SingleTagSectionHandler—— 〉 System.Collections.IDictionary
代码示例
1
private
void
testButton_Click(
object
sender, EventArgs e)
2 {
3 NameValueCollection section1 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection1 " )
4 as System.Collections.Specialized.NameValueCollection;
5 string name1 = section1[ " name " ];
6 string sender1 = section1[ " sender " ];
7 string action1 = section1[ " action " ];
8
9 Hashtable section2 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection2 " )
10 as System.Collections.Hashtable;
11 string name2 = section2[ " name " ].ToString();
12 string sender2 = section2[ " sender " ].ToString();
13 string action2 = section2[ " action " ].ToString();
14
15 IDictionary section3 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection3 " )
16 as System.Collections.IDictionary;
17 string name3 = section3[ " name " ].ToString();
18 string sender3 = section3[ " sender " ].ToString();
19 string action3 = section3[ " action " ].ToString();
20
21 this .showTextBox.Text = name1 + sender1 + action1 + " \r\n "
22 + name2 + sender2 + action2 + " \r\n "
23 + name3 + sender3 + action3 + " \r\n " ;
24 }
2 {
3 NameValueCollection section1 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection1 " )
4 as System.Collections.Specialized.NameValueCollection;
5 string name1 = section1[ " name " ];
6 string sender1 = section1[ " sender " ];
7 string action1 = section1[ " action " ];
8
9 Hashtable section2 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection2 " )
10 as System.Collections.Hashtable;
11 string name2 = section2[ " name " ].ToString();
12 string sender2 = section2[ " sender " ].ToString();
13 string action2 = section2[ " action " ].ToString();
14
15 IDictionary section3 = System.Configuration.ConfigurationManager.GetSection( " mySectionGroup/mySection3 " )
16 as System.Collections.IDictionary;
17 string name3 = section3[ " name " ].ToString();
18 string sender3 = section3[ " sender " ].ToString();
19 string action3 = section3[ " action " ].ToString();
20
21 this .showTextBox.Text = name1 + sender1 + action1 + " \r\n "
22 + name2 + sender2 + action2 + " \r\n "
23 + name3 + sender3 + action3 + " \r\n " ;
24 }