zqwuwei的技术博客

理论指导实践,在实践中更好的理解理论
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

读取配置文件的节点

Posted on 2012-09-20 22:54  zqwuwei  阅读(229)  评论(0编辑  收藏  举报
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ConsoleApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <ConsoleApplication2.Properties.Settings>
            <setting name="ConsoleApplication2_localhost_Service1" serializeAs="String">
                <value>http://localhost:4214/Service1.svc</value>
            </setting>
        </ConsoleApplication2.Properties.Settings>
    </applicationSettings>
</configuration>

C#代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            object obj = ConfigurationManager.GetSection("ConsoleApplication2.Properties.Settings");
            Configuration config = ConfigurationManager.OpenExeConfiguration(
                 ConfigurationUserLevel.None);
         
            ConfigurationSectionGroupCollection group = config.SectionGroups;

            ConfigurationSectionGroup sectionGroup = group.Get("applicationSettings");
            ConfigurationSection section = sectionGroup.Sections[0];
            string strXml = section.SectionInformation.GetRawXml();          
            
        }
    }