<configuration> <configSections> <section name="Test1" type="System.Configuration.SingleTagSectionHandler"/> <section name="Test2" type="System.Configuration.DictionarySectionHandler"/> <section name="Test3" type="System.Configuration.NameValueSectionHandler" /> </configSections> <Test1 setting1="Hello" setting2="World"/> <Test2> <add key="Hello" value="World" /> </Test2> <Test3> <add key="Hello" value="World" /> </Test3> </configuration>
我们对上面的自定义配置节进行说明。在声明部分使用<section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>声明了一个配置节它的名字叫Test1,类型为SingleTagSectionHandler。在设置配置节部分使用 <Test1 setting1="Hello" setting2="World"/>设置了一个配置节,它的第一个设置的值是Hello,第二个值是World,当然还可以有更多。其它的两个配置节和这个类似。 下面我们看在程序中如何访问这些自定义的配置节。我们用过ConfigurationSettings类的静态方法GetConfig来获取自定义配置节的信息。
public static object GetConfig(string sectionName);
下面是访问这三个配置节的代码:
//访问配置节Test1 IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1"); string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"]; MessageBox.Show(str); //输出Hello World
//访问配置节Test1的方法2 string[] values1=new string[IDTest1.Count]; IDTest1.Values.CopyTo(values1,0); MessageBox.Show(values1[0]+" "+values1[1]); //输出Hello World //访问配置节Test2
上一篇:轻松改变Foxmail邮箱帐户的存放位置
下一篇:.net中发mail到hotmail中乱码问题的解决
|