获取App.Config中自定义的配置节点的信息

C#代码

View Code
 1 static void GetConfigInfo()
 2 {
 3     //访问Test1
 4     //IDictionary idTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
 5     IDictionary idTest1 = (IDictionary)System.Configuration.ConfigurationManager.GetSection("Test1");
 6     string str = (string)idTest1["setting1"] + "_____" + (string)idTest1["setting2"];
 7     Console.WriteLine(str);
 8 
 9     //访问配置节 Test2
10     IDictionary idTest2 = (IDictionary)System.Configuration.ConfigurationManager.GetSection("Test2");            
11     string[] keys = new string[idTest2.Keys.Count];
12     string[] values = new string[idTest2.Keys.Count];
13     idTest2.Keys.CopyTo(keys, 0);
14     idTest2.Values.CopyTo(values, 0);
15     Console.WriteLine(keys[0] + "_______________" + values[0]);
16 }

 

配置文件信息

View Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!--声明一个配置节,它的名字叫Test1,类型为...再设置配置节部分使用 <Test1 setting1= "Hello" setting2="World"/>它的第一个设置的值是Hello,第二个值是World,当然还可以更多-->
    <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">
  </Test1>
  <Test2>
    <add key ="Hello1" value ="World"></add>
    <add key ="Hello2" value ="World"></add>
    <add key ="Hello3" value ="World"></add>
    <add key ="Hello4" value ="World"></add>
  </Test2>
  <Test3>
    <add key="Hello" value ="World"></add>
  </Test3>
  
  <appSettings>
    <add key="No1" value="GoldBuilder"/>
    <add key="No2" value="NormalBuilder"/>
  </appSettings>


</configuration>

 

 

posted @ 2013-04-24 12:02  pnljs  阅读(443)  评论(0)    收藏  举报