服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - .net 读取非标准配置文件的小例子

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

2019-11-15 13:46asp.net教程网 ASP.NET教程

这篇文章介绍了.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");  

延伸 · 阅读

精彩推荐