c# NameValueCollection类读取配置信息
更新时间:2009年04月27日 02:03:35 作者:
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”
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”
相关文章
.NetCore Web Api 利用ActionFilterAttribute统一接口返回值格式及问题解析
在实际项目开发过程中,统一API返回值格式对前端或第三方调用将是非常必要的,在.NetCore中我们可以通过ActionFilterAttribute来进行统一返回值的封装,对.NetCore Web Api 统一接口返回值格式相关知识感兴趣的朋友一起看看吧2022-03-03Coolite Cool Study 1 在Grid中用ComboBox 来编辑数据
作为Coolite的第一个教程,我想展现给大家能够体现Coolite强大的例子(当然也比官方例子稍微复杂一点)。2009-05-05详解ASP.NET MVC下的异步Action的定义和执行原理
这篇文章主要介绍了详解ASP.NET MVC下的异步Action的定义和执行原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-12-12ASP.NET实现MVC中获取当前URL、controller及action的方法
这篇文章主要介绍了ASP.NET实现MVC中获取当前URL、controller及action的方法,结合实例形式分析了asp.net mvc获取当前URL、controller及action的具体实现技巧,需要的朋友可以参考下2017-02-02
最新评论