2015-08-27 59 views
2

我试图修改2个.exes以从1个位置加载DevExpress dll。更改.NET应用程序的程序集解析位置

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

我该如何做到这一点?

enter image description here

enter image description here

回答

4

您可以处理AppDomain.AssemblyResolve事件,并从自己使用Assembly.LoadFile给FULLPATH它试图解决的程序集的目录加载的程序集。

实施例:

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

这是我的Program.cs文件:pastebin.com/Qkr08HCv如果我把所有他的DevExpress的文件到文件夹DevExpress的和它以外运行Form1上,它不除非我将dll放回与表单相同的位置,否则打开。我在Main()的最顶端放置了一个MessageBox(“Test”),当.dlls与exe不在同一个文件夹时它不会触发,所以我无法调用AppDomain.CurrentDomain.AssemblyResolve + = CurrentDomain_AssemblyResolve;从那里 – Kyle

+0

这取决于它何时试图加载这些类型。您必须能够在必须解决DevExpress类型之前注册事件处理程序。 – Jim

+0

它看起来像是因为您直接在您的Main方法中引用DevExpress类型,所以它必须在AssemblyResolve处理程序注册之前加载这些类型 – Jim

1

你可以设置在assemblyBinding文件夹(或多个)路径>探测::在的app.config为公共语言运行库privatePath标签加载程序集时进行搜索。 这样的代码

Reference MSDN

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <probing privatePath="libs" /> 
    </assemblyBinding> 
</runtime> 
</configuration>