.Net配置文件常用配置说明
2024-07-10 12:58:40
供稿:网友
配置文件内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configsections>
<section name="mysection" type="system.configuration.namevaluesectionhandler"/>
<section name="mysingletagsection" type="system.configuration.singletagsectionhandler"/>
<section name="mydictionarysection" type="system.configuration.dictionarysectionhandler"/>
<sectiongroup name="mysections">
<section name="mysection1" type="system.configuration.namevaluesectionhandler"/>
<section name="mysection2" type="system.configuration.namevaluesectionhandler"/>
</sectiongroup>
</configsections>
<mysection>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
</mysection>
<mysingletagsection setting1="value1" setting2="value2" setting3="value3" />
<mydictionarysection>
<add key="dictionarysectionkey1" value="dictionarysectionvalue1"/>
<add key="dictionarysectionkey2" value="dictionarysectionvalue2"/>
<add key="dictionarysectionkey3" value="dictionarysectionvalue3"/>
</mydictionarysection>
<mysections>
<mysection1>
<add key="mysection1_key1" value="mysection1_value1" />
<add key="mysection1_key2" value="mysection1_value2" />
</mysection1>
<mysection2>
<add key="mysection2_key1" value="mysection2_value1" />
<add key="mysection2_key2" value="mysection2_value2" />
</mysection2>
</mysections>
<appsettings>
<add key="appkey1" value="appvalue1" />
<add key="appkey2" value="appvalue2" />
</appsettings>
</configuration>
代码片断如下:
private static void testappsettings()
{
console.writeline("testappsettings ...");
namevaluecollection config = configurationsettings.appsettings;
console.writeline("value1:" + config["key1"]);
console.writeline("value2:" + config["key2"]);
console.writeline();
}
private static void testsection()
{
console.writeline("testsection ...");
namevaluecollection config ;
config = (namevaluecollection)configurationsettings.getconfig("mysection");
console.writeline("value1:" + config["key1"]);
console.writeline("value2:" + config["key2"]);
console.writeline();
}
private static void testsingletagsection()
{
console.writeline("testsingletagsection ...");
idictionary dic ;
dic = (idictionary)configurationsettings.getconfig("mysingletagsection");
console.writeline("value1:" + dic["setting1"]);
console.writeline("value2:" + dic["setting2"]);
console.writeline("value3:" + dic["setting3"]);
console.writeline();
}
private static void testdictionarysection()
{
console.writeline("testdictionarysection ...");
idictionary dic ;
dic = (idictionary)configurationsettings.getconfig("mydictionarysection");
console.writeline("dictionarysectionvalue1:" + dic["dictionarysectionkey1"]);
console.writeline("dictionarysectionvalue2:" + dic["dictionarysectionkey2"]);
console.writeline("dictionarysectionvalue3:" + dic["dictionarysectionkey3"]);
console.writeline();
}
private static void testsectiongroup()
{
console.writeline("testsectiongroup ...");
namevaluecollection config1 ;
namevaluecollection config2 ;
config1 = (namevaluecollection)configurationsettings.getconfig("mysections/mysection1");
config2 = (namevaluecollection)configurationsettings.getconfig("mysections/mysection2");
console.writeline("mysections/mysections1/value1:" + config1["mysection1_key1"]);
console.writeline("mysections/mysections1/value2:" + config1["mysection1_key2"]);
console.writeline("mysections/mysections2/value1:" + config2["mysection2_key1"]);
console.writeline("mysections/mysections2/value2:" + config2["mysection2_key2"]);
console.writeline();
}
菜鸟学堂: