代碼如下:
復制代碼 代碼如下:
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");
您可能感興趣的文章:- asp.net 在處理向該請求提供服務所需的配置文件時出錯
- .NET 2.0獲取配置文件AppSettings和ConnectionStrings節數據的方法
- 認識ASP.NET配置文件Web.config