我有一个应用程序需要以 dll 的形式加载附加组件。 dll 需要从配置 (app.config) 文件中获取其配置信息。我想动态找出 app.config 文件的名称,据我所知,这样做的方法是 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
但是,由于它托管在父应用程序内部,因此从上述代码段获得的配置文件是 (parentapplication).exe.config。我无法在父应用程序中加载另一个 appdomain,但我想更改 appdomain 的配置文件详细信息。我应该怎么做才能获取 dll 的配置文件?
最佳答案
好的,最后,我设法将一些对我有用的东西组合在一起。也许这会有所帮助;
使用 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 { }
关于file - 如何在另一个应用程序域中加载 dll 的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12112489/