当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > c# NameValueCollection类读取配置信息

ASP.NET
Web服务器控件:LinkButton控件
Web服务器控件:ListBox控件
Web服务器控件:ListItem控件
Web服务器控件:Literal控件
Web服务器控件:Panel控件
Web服务器控件:PlaceHolder控件
Web服务器控件:RadioButton控件
Web服务器控件:RadioButtonList控件
Web服务器控件:BulletedList控件
Web服务器控件:Style控件
Web服务器控件:Table控件
Web服务器控件:TableCell控件
Web服务器控件:TableRow控件
Web服务器控件:TextBox控件
Web服务器控件:XML控件
Validation服务器控件:CompareValidator控件
Validation服务器控件:CustomValidator控件
Validation服务器控件:RangeValidator控件
Validation服务器控件:RegularExpressionValidator控件
Validation服务器控件:RequiredFieldValidator控件

ASP.NET 中的 c# NameValueCollection类读取配置信息


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 157 ::
收藏到网摘: n/a

c#中的NameValueCollection类读取配置信息,大家可以参考下。 我首先介绍配置文件中的写法:
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”