当前位置:编程学习 > asp >>

.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"); 

 

摘自 tommy的专栏

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,