欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# NameValueCollection類讀取配置信息

 更新時間:2009年04月27日 02:03:35   作者:  
c#中的NameValueCollection類讀取配置信息,大家可以參考下。
我首先介紹配置文件中的寫法:
1.在VS2005中的工程下建立一個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)部添加兩個測試項(xiàng)“<add key="IPAddress" value="127.0.0.1"/>”和“<add key="Port" value="13000"/>”;配置文件定義完畢。

2.打開要讀取配置信息的代碼文件,添加兩個引用,分別是:
復(fù)制代碼 代碼如下:

using System.Configuration;
using System.Collections.Specialized;

定義一個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”

相關(guān)文章

最新評論