.net 读取非标准配置文件的小例子
程序员文章站
2024-02-28 21:57:58
代码如下:复制代码 代码如下:public static string config(string key) &...
代码如下:
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");