用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

.net 读取非标准配置文件的小例子

时间:2015-01-29  来源:互联网  作者:佚名
代码如下:
复制代码 代码如下:
public static string Config(string key) 
       { 
           ExeConfigurationFileMap file = new ExeConfigurationFileMap(); 
           file.ExeConfigFilename = @"Providers\\Provider.config"; 
           Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); 
           AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings"); 
           return appsection.Settings[key].Value; 
       } 

配置文件目录结构:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
        <add key="ConnectionString" value="Server=(local);Database=DB;User Id=sa;Password=123" /> 
    </appSettings> 
</configuration> 

调用:
复制代码 代码如下:
//里面的参数为配置文件的key  
string strConn=Config("ConnectionString"); 
来顶一下
返回首页
返回首页
推荐资讯
C# 实现抓取网站页面内容的实例方法 C# 实现抓取网站页面内容的实例方法抓取新浪网的新闻栏目,如图所示:使用 谷歌浏览器的查看源代
vs2010根据字符串内容添加断点的方法介绍 vs2010根据字符串内容添加断点的方在vs中我们可以直接用表达式。数值型比较直接用操作符即可。
相关文章
栏目更新
栏目热门