c# NameValueCollection類(lèi)讀取配置信息
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 22:44:25
c# NameValueCollection類(lèi)讀取配置信息
c# NameValueCollection類(lèi)讀取配置信息:我首先介紹配置文件中的寫(xiě)法: 1.在VS2005中的工程下建立一個(gè)config文件,名稱(chēng)為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>
導(dǎo)讀c# NameValueCollection類(lèi)讀取配置信息:我首先介紹配置文件中的寫(xiě)法: 1.在VS2005中的工程下建立一個(gè)config文件,名稱(chēng)為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>

我首先介紹配置文件中的寫(xiě)法:
1.在VS2005中的工程下建立一個(gè)config文件,名稱(chēng)為App.config,并如下編輯:
代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="StartParameters"
type="System.Configuration.NameValueSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<StartParameters>
<add key="IPAddress" value="127.0.0.1"/>
<add key="Port" value="13000"/>
</StartParameters>
</configuration>
其中section節(jié)點(diǎn)的name值是自己定義的,在此我定義為“StartParameters”,然后添加上方聲明的節(jié)點(diǎn),并在節(jié)點(diǎn)內(nèi)部添加兩個(gè)測(cè)試項(xiàng)“<add key="IPAddress" value="127.0.0.1"/>”和“<add key="Port" value="13000"/>”;配置文件定義完畢。
2.打開(kāi)要讀取配置信息的代碼文件,添加兩個(gè)引用,分別是:
代碼如下:
using System.Configuration;
using System.Collections.Specialized;
定義一個(gè)NameValueCollection類(lèi)型的變量:
代碼如下:
NameValueCollection _table = null;
_table = (NameValueCollection)ConfigurationManager.GetSection("StartParameters");
String ipAddress =_table["IPAddress"].ToString();
String port = _table["Port"].ToString();
上句中的“StartParameters”就是在配置文件中定義的name值。
輸出ipAddress 和port 的值,分別是:
代碼如下:
“127.0.0.1”
“13000”
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
c# NameValueCollection類(lèi)讀取配置信息
c# NameValueCollection類(lèi)讀取配置信息:我首先介紹配置文件中的寫(xiě)法: 1.在VS2005中的工程下建立一個(gè)config文件,名稱(chēng)為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>