.net 读取非标准配置文件的小例子
时间:2021-12-31 09:08:02|栏目:.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");
上一篇:.net core如何在网络高并发下提高JSON的处理效率详解
栏 目:.NET代码
下一篇:asp.net IList查询数据后格式化数据再绑定控件
本文标题:.net 读取非标准配置文件的小例子
本文地址:http://www.codeinn.net/misctech/188675.html