从不同的文件夹加载一个DLL引用? [英] load a DLL reference from a different folder?

查看:219
本文介绍了从不同的文件夹加载一个DLL引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET控制台应用程序引用一个DLL。如果DLL与EXE位于同一个文件夹中,但是我想将DLL放在不同的文件夹中,则运行正常。如何做到这一点?

解决方案

如果dll在一个子文件夹中,可以将此文件夹添加到 AppDomain专用路径



如果您不想创建新的AppDomain,AppDomain已创建(AppDomain.AppendPrivatePath已过时),AppDomain的私有bin路径无法更改,您可以修改探测元素在App.config。



如果它不在一个子文件夹中,事情变得更复杂,你可以使用其完整路径加载程序集,但是如果引用其他程序集,则CLR将无法解析依赖关系。



如果你想解决依赖关系,你可以添加一个处理程序到 AppDomain.AssemblyResolve 并在您的特定文件夹中获取所需的程序集。



另一种可能性是将此dll(和依赖项)放在GAC中。


I have a .NET console app that references a DLL. It runs fine if the DLL is in the same folder as the EXE but I would like to put the DLL in a different folder. How can I do that?

解决方案

If the dll is in a sub folder you can add this folder to the AppDomain private path.

The private bin path of an AppDomain cannot be changed once the AppDomain has been created (AppDomain.AppendPrivatePath is obsolete), if you don't want to create a new AppDomain, you can modify the probing element in the App.config.

If it's not in a sub folder, things get more complicated, you can load the assembly using its full path, but if it references other assembly, the CLR won't be able to resolve the dependencies.

If you want to resolve dependencies, you can add an handler to AppDomain.AssemblyResolve and fetch the needed assembly in your specific folder.

Another possibility is to place this dll (and dependencies) in the GAC.

这篇关于从不同的文件夹加载一个DLL引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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