這篇文章介紹了.net 讀取非標准配置文件的小例子,有需要的朋友可以參考一下
代碼如下:
復制代碼 代碼如下:
public static string Config(string key)
{
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = @"ProvidersProvider.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");