如何在另一个应用程序域中加载DLL配置文件

4
我有一个应用程序需要加载一个dll形式的插件。该dll需要从配置文件(app.config)中获取其配置信息。我想动态地查找app.config文件的名称,根据我的理解,这可以通过AppDomain.CurrentDomain.SetupInformation.ConfigurationFile实现。
然而,由于它被托管在父应用程序内部,从上述代码中获得的配置文件是(parentapplication).exe.config。我无法在父应用程序内部加载另一个appdomain,但我想更改appdomain的配置文件详细信息。我该如何处理才能获取dll的配置文件?

我目前也遇到了同样的问题,我认为没有解决方案。请参阅此线程,该问题更或多或少相同;http://stackoverflow.com/questions/636275/appdomain-and-config-section-typing - RJ Lohan
1个回答

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 { }

找到了一种更短的将URI转换为本地路径的方法:var dllpath = (new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath; - Endy Tjahjono

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接