.net 读取非标准配置文件的小例子
.net 读取非标准配置文件的小例子
发布时间:2016-12-29 来源:查字典编辑
摘要:代码如下:复制代码代码如下:publicstaticstringConfig(stringkey){ExeConfigurationFile...

代码如下:

复制代码 代码如下:

public static string Config(string key)

{

ExeConfigurationFileMap file = new ExeConfigurationFileMap();

file.ExeConfigFilename = @"ProvidersProvider.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教程学习
热门asp.net教程学习
编程开发子分类