程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .net 讀取非標准配置文件的小例子

.net 讀取非標准配置文件的小例子

編輯: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"); 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved