posted by 방랑군 2009. 9. 23. 14:24



Understanding Section Handlers - App.config File

참조 : http://www.codeproject.com/KB/aspnet/ConfigSections.aspx


예제 Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

//1 SECTIOIN 구성등

<configSections>

<sectionGroup name="settings">

<section name="dbSettings" type="System.Configuration.NameValueSectionHandler"/>

</sectionGroup>

<sectionGroup name="inputScreen">

<section name="education" type="System.Configuration.DictionarySectionHandler"/>

<section name="sex" type="System.Configuration.SingleTagSectionHandler"/>

</sectionGroup>

<sectionGroup name="companyInfo">

<section name="companyAddress" type="ConfigSections.MyConfigHandler,ConfigSections"/>

</sectionGroup>

</configSections>


<settings>

<dbSettings>

<add key="connectionString" value="Data Source=localhost;Initial Catalog=Northwind;"/>

<add key="imagesPath" value="c:\MyApp\Resources\Images\"/>

</dbSettings>

</settings>


<inputScreen>

<education>

<add key="1" value="High School"/>

<add key="2" value="Bachelors"/>

<add key="3" value="Masters"/>

<add key="4" value="Doctorate"/>

</education>

<sex Male="1" Female="2"/>

</inputScreen>


<companyInfo> 

<companyAddress> 

<companyName>Axxonet Solutions India Pvt Ltd</companyName>

<doorNo>1301</doorNo>

<street>13th Cross, Indira Nagar, 2nd Stage</street>

<city>Bangalore</city>

<postalCode>560038</postalCode>

<country>India</country>

</companyAddress>

</companyInfo>


<!--

Read appsettings from an external file.

-->

<appSettings file="appSettings.xml"/>

<appSettings>

<add key="author" value="Palanisamy Veerasingam"/>

<add key="article" value="Configuration Sections"/>

</appSettings>

</configuration>


.NET 1.1 호출 밥법

NameValueCollection nvc=(NameValueCollection)ConfigurationSettings.GetConfig("settings/dbSettings");

            MessageBox.Show("Connection String is " + nvc[0].ToString() );



Hashtable objHash=(Hashtable) ConfigurationSettings.GetConfig("inputScreen/education");

MessageBox.Show("Education list count is " + objHash.Count);


Hashtable objHash=(Hashtable) ConfigurationSettings.GetConfig("inputScreen/sex");

MessageBox.Show("Value of male is " + objHash["Male"].ToString() );

CCompanyAddr obj=(CCompanyAddr)ConfigurationSettings.GetConfig("companyInfo/companyAddress"); 

string strMsg;

strMsg = "Company Address is:\n" +

 "\n" + obj.CompanyName +

 "\n" + obj.DoorNo + ", " + obj.Street +

 "\n" + obj.City + 

     "\n" + obj.Country;

MessageBox.Show(strMsg);



string strAuthor=ConfigurationSettings.AppSettings["author"];

MessageBox.Show("This sample is coded by " + strAuthor);



.NET 2.0...