简体   繁体   English

如何在Visual Studio / C#中为DLL依赖项定义工作目录

[英]How to define working directories for DLL dependencies in Visual Studio / C#

I've got an executeable file, which I run for example from C:. 我有一个可执行文件,例如从C:运行。 The executable references some DLLs from another directory, let's say C:\\MyDLLs. 该可执行文件引用另一个目录中的某些DLL,例如C:\\ MyDLLs。 The problem is, that these referenced DLLs again depend on other DLLs, which are stored in another directory. 问题是,这些引用的DLL再次依赖于存储在另一个目录中的其他DLL。 Can I tell Visual Studio where to look for these missing DLLs? 我可以告诉Visual Studio在哪里寻找这些丢失的DLL吗? Thanks a lot! 非常感谢!

You can reference assemblies outside of your app's assembly loading rules by setting these values in configuration. 您可以通过在配置中设置这些值来引用应用程序的程序集加载规则之外的程序集。 Here's a sample configuration file from this Microsoft KB article : 这是此Microsoft KB文章中的示例配置文件:

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
            <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

You use the <codeBase> element to tell your app where to look. 您使用<codeBase>元素告诉您的应用程序在哪里看。

You have to make the assembly strong named (use the sn.exe tool) for this to work. 您必须使程序集具有较强的名称(使用sn.exe工具)才能正常工作。

It's also useful to understand how the runtime resolves assembly references and maybe you can take advantage of that instead of going through all the hoops to use <codeBase> . 了解运行时如何解析程序集引用也很有用,也许您可​​以利用它来代替使用所有代码来使用<codeBase>

I've had this issue before and I created a post build script that copies all the needed DLLs into my executable's directory. 我以前遇到过此问题,并且创建了一个后生成脚本,该脚本将所有需要的DLL复制到可执行文件的目录中。

Something like: copy "$(ProjectDir)Resources\\DLLs\\yourDLL.dll" "$(TargetDir)yourDLL.dll" 类似于:复制“ $(ProjectDir)Resources \\ DLLs \\ yourDLL.dll”“ $(TargetDir)yourDLL.dll”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM