.Net配置文件常用配置说明

 

配置文件内容如下:

<?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();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 VB.NET 中,可以使用 App.config 文件来存储应用程序的配置信息。App.config 文件是一个 XML 文件,可以包含多个配置节和配置项。以下是一些常用配置节和配置项: 1. appSettings 配置节:用于存储应用程序的键值对配置项,例如: ``` <configuration> <appSettings> <add key="ConnectionString" value="Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True" /> <add key="LogLevel" value="Debug" /> </appSettings> </configuration> ``` 2. connectionStrings 配置节:用于存储数据库连接字符串配置项,例如: ``` <configuration> <connectionStrings> <add name="MyDatabase" connectionString="Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> ``` 3. system.web 配置节:用于存储 Web 应用程序的配置项,例如: ``` <configuration> <system.web> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" /> </system.web> </configuration> ``` 在代码中,可以使用 ConfigurationManager 类来访问配置文件中的配置项的值,例如: ``` Dim connectionString As String = ConfigurationManager.AppSettings("ConnectionString") Dim logLevel As String = ConfigurationManager.AppSettings("LogLevel") Dim myDatabaseConnectionString As String = ConfigurationManager.ConnectionStrings("MyDatabase").ConnectionString ``` 注意:配置文件的名称应该是 App.config,需要放置在项目的根目录下。如果你需要在Web应用程序中使用配置文件,那么名称应该是Web.config,需要放置在网站的根目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值