2012-08-24 110 views
4

我有一個應用程序需要以dll的形式加載加載項。該DLL需要從配置(app.config)文件中獲取其配置信息。我想動態地找出app.config文件的名稱,據我所知,這樣做的方法是AppDomain.CurrentDomain.SetupInformation.ConfigurationFile如何在另一個應用程序域中加載dll的配置文件

但是,由於它是託管在一個父應用程序中,所以配置從上面的代碼中得到的文件是(parentapplication).exe.config。我無法在父應用程序中加載另一個appdomain,但我想更改appdomain的配置文件詳細信息。我應該怎麼做才能得到dll的配置文件?

+0

我現在有同樣的問題,我不相信有一個解決方案。看到這個線程或多或少是同一個問題; http://stackoverflow.com/questions/636275/appdomain-and-config-section-typing –

回答

3

好的,最後,我設法一起破解一些適合我的東西。也許這會有所幫助;

使用Assembly.GetExecutingAssembly,從具有我想要讀取的配置文件的DLL中,我可以使用.CodeBase在爲它啓動一個新的AppDomain之前找到DLL的位置。 * .dll .config位於同一個文件夾中。

然後必須轉換URI(如.CodeBase看起來像「file://path/assembly.dll」)以獲得ConfigurationManager的LocalPath(它不喜歡Uri格式化的字符串)。

try 
{ 
    string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; 
    string originalAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 
    Uri uri = new Uri(String.Format("{0}\\{1}.dll", originalAssemblyPath, assemblyName)); 
    string dllPath = uri.LocalPath; 

    configuration = ConfigurationManager.OpenExeConfiguration(dllPath); 
} 
catch { } 
+0

找到一個更短的方式來轉換[URI到本地路徑](http://stackoverflow.com/a/2442170/196451): 'var dllpath =(new Uri(System.Reflection.Assembly.GetExecutingAssembly()。CodeBase))。LocalPath;' –

相關問題