2009-07-28 22 views
5

我期待將標準 .Net ConfigurationManager類重定向到另一個文件; 完全。路徑是在運行時確定的,所以我不能使用configSource等(這不是重複的問題 - 我看了其他人)。將ConfigurationManager重定向到另一個文件

我基本上試圖複製ASP.Net正在做的內容。因此,不僅我的類應該從新的配置文件中讀取,而且還要讀取任何標準的.Net內容(我特意試圖使用的是system.codeDom元素)。

我已經破解了反射器,並開始研究ASP.Net是如何做到的 - 它非常多毛,完全沒有文檔。我希望有人對這個過程進行了逆向工程。不一定要尋找一個完整的解決方案(很好),但僅僅是文檔

回答

9

我終於明白了。有一個公開的記錄意味着做到這一點 - 但它隱藏在.Net框架的深處。改變你自己的配置文件需要反射(只需刷新ConfigurationManager);但可以更改通過公共API創建的AppDomain的配置文件。

沒有感謝微軟連接功能,我提交的,這裏是代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     // Setup information for the new appdomain. 
     AppDomainSetup setup = new AppDomainSetup(); 
     setup.ConfigurationFile = "C:\\my.config"; 

     // Create the new appdomain with the new config. 
     AppDomain d2 = AppDomain.CreateDomain("customDomain", AppDomain.CurrentDomain.Evidence, setup); 

     // Call the write config method in that appdomain. 
     CrossAppDomainDelegate del = new CrossAppDomainDelegate(WriteConfig); 
     d2.DoCallBack(del); 

     // Call the write config in our appdomain. 
     WriteConfig(); 

     Console.ReadLine(); 
    } 

    static void WriteConfig() 
    { 
     // Get our config file. 
     Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

     // Write it out. 
     Console.WriteLine("{0}: {1}", AppDomain.CurrentDomain.FriendlyName, c.FilePath); 
    } 
} 

輸出:

customDomain: C:\my.config 
InternalConfigTest.vshost.exe: D:\Profile\...\InternalConfigTest.vshost.exe.config 
相關問題