|
1.在VS2005中的工程下建立一個(gè)config文件,名稱為App.config,并如下編輯:
復(fù)制代碼 代碼如下:
<?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è)測試項(xiàng)“<add key="IPAddress" value="127.0.0.1"/>”和“<add key="Port" value="13000"/>”;配置文件定義完畢。
2.打開要讀取配置信息的代碼文件,添加兩個(gè)引用,分別是:
復(fù)制代碼 代碼如下:
using System.Configuration;
using System.Collections.Specialized;
定義一個(gè)NameValueCollection類型的變量:
復(fù)制代碼 代碼如下:
NameValueCollection _table = null;
_table = (NameValueCollection)ConfigurationManager.GetSection("StartParameters");
String ipAddress =_table["IPAddress"].ToString();
String port = _table["Port"].ToString();
上句中的“StartParameters”就是在配置文件中定義的name值。
輸出ipAddress 和port 的值,分別是:
復(fù)制代碼 代碼如下:
“127.0.0.1”
“13000”
AspNet技術(shù):c# NameValueCollection類讀取配置信息,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。