2012-08-24 35 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;' –

相关问题