将自定义路径设置为引用的 DLL? [英] Set Custom Path to Referenced DLL's?

查看:32
本文介绍了将自定义路径设置为引用的 DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 项目(称为 MainProj),它引用了其他几个 DLL 项目.通过将这些项目添加到 MainProj 的引用,它会构建它们并将它们生成的 DLL 复制到 MainProj 的工作目录.

我想做的是将这些引用的 DLL 放在 MainProj 工作目录的子目录中,即 MainProj/bin/DLLs,而不是工作目录本身.

我不是一个非常有经验的 C# 程序员,但来自 C++ 世界,我假设一种方法是删除项目引用并通过路径和文件名显式加载所需的 DLL(即在 C++ 中,LoadLibrary).但是,如果有办法,我更愿意做的是设置某种参考二进制路径",这样当我构建时它们都会被自动复制到这个子目录(然后从那里被引用而没有我需要明确加载每个).这种事情可能吗?

如果没有,在 C# 中完成我所追求的首选方法是什么(即带有 Assembly.Load/Assembly.LoadFile/Assembly.LoadFrom?也许是 AppDomain 中的东西,或者 System.Environment?)

解决方案

来自本页(未经我测试):

在程序初始化的某处(在从引用的程序集中访问任何类之前)执行以下操作:

AppDomain.CurrentDomain.AppendPrivatePath(@binDLLs");

这篇文章 表示 AppendPrivatePath 已过时,但也提供了解决方法.

编辑 2: 看起来最简单、最犹太的方法是在 app.config 文件中(请参阅 这里):

<预><代码><配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><probing privatePath="binDLLs";/></assemblyBinding></运行时></配置>

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.

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.

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?

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?)

解决方案

From this page (untested by me):

Somewhere in your program's initialization (before you access any classes from a referenced assembly) do this:

AppDomain.CurrentDomain.AppendPrivatePath(@"binDLLs");

Edit: This article says AppendPrivatePath is considered obsolete, but also gives a workaround.

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="binDLLs" />
    </assemblyBinding>
  </runtime>
</configuration>

这篇关于将自定义路径设置为引用的 DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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