.net 读取非标准配置文件的小例子
更新时间:2013年07月30日 10:22:01 作者:
这篇文章介绍了.net 读取非标准配置文件的小例子,有需要的朋友可以参考一下
代码如下:
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");
复制代码 代码如下:
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");
相关文章
ASP.NET MVC HttpPostedFileBase文件上传的实例代码
这篇文章主要介绍了ASP.NET MVC HttpPostedFileBase文件上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07asp.net 枚举文件里面的数字绑定到DropDownList里面去
将枚举文件里面的数字绑定到DropDownList里面去的实现方法,大家可以借鉴下。2009-07-07ASP.NET Core WebAPI实现本地化(单资源文件)
这篇文章主要介绍了ASP.NET Core WebAPI实现本地化(单资源文件),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-06-06详解.Net core2.0日志组件Log4net、Nlog简单性能测试
这篇文章主要介绍了详解.Net core2.0日志组件Log4net、Nlog简单性能测试,比较log4net、nlog的文件写入性能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-07-07
最新评论