改变.NET应用程序的组件解决位置 [英] Change .NET App's Assembly Resolve Location

查看:90
本文介绍了改变.NET应用程序的组件解决位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改2 .EXE文件,从1位置加载DLL的DevExpress的

I am trying to modify 2 .exes to load DevExpress dlls from 1 location.

在产品的.EXE文件的文件夹都使用相同的.dll为发射器一样。我想避免把相同的.dll文件到产品目录中,而是有1回目录读取.EXE文件(该发射器目录)。

The .exes in the "Products" folder are use the same .dlls as the launcher does. I want to avoid having to put the same .dlls into the Products directory, and instead have the .exes read from 1 directory back(the launchers directory).

如何能我做到这一点?

推荐答案

您可以处理AppDomain.AssemblyResolve事件并加载自己使用大会从目录中的程序集。 。的LoadFile给予完整路径到它试图解决组装

You can handle the AppDomain.AssemblyResolve event and load the assemblies from the directory yourself using Assembly.LoadFile giving the fullpath to the assembly it is trying to resolve.

例如:

.
.
.
// 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;
}

这篇关于改变.NET应用程序的组件解决位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆