c#中的NameValueCollection类读取配置信息,大家可以参考下。
我首先介绍配置文件中的写法:
1.在VS2005中的工程下建立一个config文件,名称为App.config,并如下编辑:
1.在VS2005中的工程下建立一个config文件,名称为App.config,并如下编辑:
代码如下:
<section
name=”StartParameters”
type=”System.Configuration.NameValueSectionHandler,System, Vers来源gaodaima#com搞(代@码网ion=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″/>
其中section节点的name值是自己定义的,在此我定义为“StartParameters”,然后添加上方声明的节点,并在节点内部添加两个测试项“”和“”;配置文件定义完毕。
2.打开要读取配置信息的代码文件,添加两个引用,分别是:
代码如下:
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Specialized;
定义一个NameValueCollection类型的变量:
代码如下:
NameValueCollection _table = null;
_table = (NameValueCollection)ConfigurationManager.GetSection(“StartParameters”);
String ipAddress =_table[“IPAddress”].ToString();
String port = _table[“Port”].ToString();
_table = (NameValueCollection)ConfigurationManager.GetSection(“StartParameters”);
String ipAddress =_table[“IPAddress”].ToString();
String port = _table[“Port”].ToString();
上句中的“StartParameters”就是在配置文件中定义的name值。
输出ipAddress 和port 的值,分别是:
代码如下:
“127.0.0.1”
“13000”
“13000”
以上就是c# NameValueCollection类读取配置信息的详细内容,更多请关注gaodaima搞代码网其它相关文章!