使用ConfigurationManager從任意位置加載配置我正在開(kāi)發(fā)一個(gè)數(shù)據(jù)訪問(wèn)組件,它將在一個(gè)包含經(jīng)典ASP和ASP.NET頁(yè)面的網(wǎng)站中使用,并且需要一種很好的方法來(lái)管理其配置設(shè)置。我想用一種習(xí)俗ConfigurationSection對(duì)于ASP.NET頁(yè)面來(lái)說(shuō),這非常有用。但是,當(dāng)通過(guò)COM互操作從典型的ASP頁(yè)面調(diào)用組件時(shí),組件并不在ASP.NET請(qǐng)求的上下文中運(yùn)行,因此不了解web.config。有沒(méi)有辦法告訴我們ConfigurationManager只需從任意路徑加載配置(例如,..\web.config如果我的程序集位于/bin文件夾)?如果有,那么我認(rèn)為如果默認(rèn)的話,我的組件可以回到那個(gè)位置。ConfigurationManager.GetSection回報(bào)null我的定制區(qū)。任何其他方法都是歡迎的!
3 回答

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

素胚勾勒不出你
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", <Full_Path_To_The_Configuration_File>);
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Shared\app.config");
IDisposable
using(AppConfig.Change(tempFileName)){ // tempFileName is used for the app config during this context}
- 3 回答
- 0 關(guān)注
- 491 瀏覽
添加回答
舉報(bào)
0/150
提交
取消