c# - 更改 .NET 应用程序的程序集解析位置

标签 c# .net

我正在尝试修改 2 个 .exe 以从 1 个位置加载 DevExpress dll。

“产品”文件夹中的 .exe 文件与启动程序使用相同的 .dll 文件。我想避免将相同的 .dll 放入 Products 目录,而是从 1 个目录(启动器目录)读取 .exe。

我怎样才能实现这个目标?

enter image description here

enter image description here

最佳答案

您可以处理 AppDomain.AssemblyResolve 事件,并使用 Assembly.LoadFile 自行从目录加载程序集,为其尝试解析的程序集提供完整路径。

示例:

.
.
.
// elsewhere at app startup time attach the handler to the AppDomain.AssemblyResolve event
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
.
.
.

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    AssemblyName assemblyName = new AssemblyName(args.Name);

    // this.ReadOnlyPaths is a List<string> of paths to search.
    foreach (string path in this.ReadOnlyPaths)
    {
        // If specified assembly is located in the path, use it.
        DirectoryInfo directoryInfo = new DirectoryInfo(path);

        foreach (FileInfo fileInfo in directoryInfo.GetFiles())
        {
             string fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");                    

             if (assemblyName.Name.ToUpperInvariant() == fileNameWithoutExt.ToUpperInvariant())
             {
                  return Assembly.Load(AssemblyName.GetAssemblyName(fileInfo.FullName));
             }
        }
    }
    return null;
}

关于c# - 更改 .NET 应用程序的程序集解析位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253734/

相关文章:

c# - 如何将 C# 文档生成为 CHM 或 HTML 文件?

c# - 将数据库对象从数据库拉入列表的最简洁方法是什么?

c# - 有多少 .NET 是非托管的?

c# - .Net 标准库是否支持 Windows7 WPF 应用程序?

c# - 将 `Nullable<T>` 作为类型参数传递给 C# 函数

c# - 删除测试驱动开发中的重复

c# - 如何使用 WampSharp 处理错误和连接关闭

c# - 局部变量与实例变量

c# - Redis Pub/Sub 不发布消息

c# - 使用 OR 运算符在 LINQ 表达式中添加 Between 子句