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

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

问题描述

我有一个引用 DLL 的 .NET 控制台应用程序.如果 DLL 与 EXE 位于同一文件夹中,则它运行良好,但我想将 DLL 放在不同的文件夹中.我该怎么做?

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?

推荐答案

如果 DLL 在子文件夹中,您可以将此文件夹添加到 AppDomain 私有路径.

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

一旦 AppDomain 被创建(AppDomain.AppendPrivatePath 已过时),AppDomain 的私有 bin 路径不能改变,如果你不想新建AppDomain,可以修改探测元素 在 App.config 中.

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.

如果它不在子文件夹中,事情会变得更复杂,您可以使用完整路径加载程序集,但如果它引用其他程序集,CLR 将无法解析依赖项.

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.

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

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

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

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

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

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