c# NameValueCollection類讀取配置信息
來源:懂視網
責編:小采
時間:2020-11-27 22:44:25
c# NameValueCollection類讀取配置信息
c# NameValueCollection類讀取配置信息:我首先介紹配置文件中的寫法: 1.在VS2005中的工程下建立一個config文件,名稱為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>
導讀c# NameValueCollection類讀取配置信息:我首先介紹配置文件中的寫法: 1.在VS2005中的工程下建立一個config文件,名稱為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>

我首先介紹配置文件中的寫法:
1.在VS2005中的工程下建立一個config文件,名稱為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節點的name值是自己定義的,在此我定義為“StartParameters”,然后添加上方聲明的節點,并在節點內部添加兩個測試項“<add key="IPAddress" value="127.0.0.1"/>”和“<add key="Port" value="13000"/>”;配置文件定義完畢。
2.打開要讀取配置信息的代碼文件,添加兩個引用,分別是:
代碼如下:
using System.Configuration;
using System.Collections.Specialized;
定義一個NameValueCollection類型的變量:
代碼如下:
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”
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
c# NameValueCollection類讀取配置信息
c# NameValueCollection類讀取配置信息:我首先介紹配置文件中的寫法: 1.在VS2005中的工程下建立一個config文件,名稱為App.config,并如下編輯: 代碼如下:<xml version=1.0 encoding=utf-8 > <configuration> <configSections>