【问题标题】:How do I load a dll's config file inside another appdomain如何在另一个应用程序域中加载 dll 的配置文件
【发布时间】:2012-08-24 15:36:49
【问题描述】:
我有一个应用程序需要以 dll 的形式加载插件。 dll 需要从配置 (app.config) 文件中获取其配置信息。我想动态找出 app.config 文件的名称,据我所知,这样做的方法是 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
但是,由于它是托管在父应用程序内部的,因此从上述代码中获取的配置文件是 (parentapplication).exe.config。我无法在父应用程序中加载另一个 appdomain,但我想更改 appdomain 的配置文件详细信息。我应该怎么做才能获取 dll 的配置文件?
【问题讨论】:
标签:
file
dll
configuration
exe
appdomain
【解决方案1】:
好的,最后,我设法一起破解了一些对我有用的东西。也许这会有所帮助;
使用 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 to local path:var dllpath = (new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;