如何用.net读取非标准配置文件

本篇内容主要讲解“如何用.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"); 

到此,相信大家对“如何用.net读取非标准配置文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!