设置引用DLL的自定义路径? [英] Set Custom Path to Referenced DLL's?
问题描述
我有一个C#项目(称为 MainProj
),它引用了其他几个DLL项目。通过将这些项目添加到 MainProj
的引用中,它将构建它们并将其生成的DLL复制到MainProj的工作目录。
I've got a C# project (call it MainProj
) which references several other DLL projects. By adding these projects to MainProj
's references, it will build them and copy their resulting DLL's to MainProj's working directory.
我想做的是将这些引用的DLL定位在 MainProj
的工作目录(即MainProj / bin / DLL)的子目录中,而不是工作目录本身。
What I'd like to do is have these referenced DLL's be located in a subdirectory of MainProj
's working directory, i.e. MainProj/bin/DLLs, rather than the working directory itself.
我不是一个非常有经验的C#程序员,但来自C ++世界,我假设一种方法是删除项目引用并明确通过路径和文件名加载所需的DLL(即在C ++中, LoadLibrary
)。但是,如果有一种方法,那么我想要设置某种参考二进制路径,所以当我构建时,它们都将被自动复制到这个子目录(然后从那里被引用,而没有我需要明确加载每个)。这样的事情可能吗?
I'm not a very experienced C# programmer, but coming from the C++ world, I'm assuming one approach would be to remove the project references and explicitly load the required DLL's by path and filename (i.e. in C++, LoadLibrary
). What I'd prefer to do however, if there's a way, would be to set some sort of "reference binary path", so they'd all be auto-copied to this subdir when I build (and then be referenced from there without me needing to explicitly load each). Is such a thing possible?
如果没有,C#中首选的方法是完成我以后的工作(即某些 Assembly.Load
/ Assembly.LoadFile
/ Assembly.LoadFrom
? AppDomain
或者 System.Environment
?)
If not, what's the preferred method in C# to accomplish what I'm after (i.e. something with Assembly.Load
/ Assembly.LoadFile
/ Assembly.LoadFrom
? Something in AppDomain
perhaps, or System.Environment
?)
推荐答案
从此页面(未经测试):
在程序的初始化过程中(在从引用程序集访问任何类之前)执行以下操作:
Somewhere in your program's initialization (before you access any classes from a referenced assembly) do this:
AppDomain.CurrentDomain.AppendPrivatePath(@"bin\DLLs");
编辑: 这篇文章说AppendPrivatePath被认为是过时的,但也给出了一个解决方法
This article says AppendPrivatePath is considered obsolete, but also gives a workaround.
编辑2:看起来像app.config文件中最简单和最犹豫的方法是这样做(见 here ):
Edit 2: Looks like the easiest and most kosher way to do this is in the app.config file (see here):
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin\DLLs" />
</assemblyBinding>
</runtime>
</configuration>
这篇关于设置引用DLL的自定义路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!