file - 如何在另一个应用程序域中加载 dll 的配置文件

标签 file dll configuration exe appdomain

我有一个应用程序需要以 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/

相关文章:

c# - 从 C# 项目制作 DLL?

java - 对于 Hibernate 的 "Could not parse configuration"错误我该怎么办?

c# - 加载自定义配置文件

PHP 无法在我的服务器上正确下载 apk 文件

带有混合输入标签的 html 表单(类型=文件和其他)

file - 我如何存储可以在Delphi中运行时更改的文件路径

c - 使用 system() 通过外部程序创建的文件与通过 open() 打开它之间需要延迟吗?

c++ - 编译DLL时如何添加include

c# - 将错误消息从 C# DLL 返回到 C# 程序?

configuration - "master_not_discovered_exception"尝试在远程服务器上配置 Elasticsearch 时