简体   繁体   中英

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:. The executable references some DLLs from another directory, let's say C:\\MyDLLs. The problem is, that these referenced DLLs again depend on other DLLs, which are stored in another directory. Can I tell Visual Studio where to look for these missing DLLs? 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 :

<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.

You have to make the assembly strong named (use the sn.exe tool) for this to work.

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> .

I've had this issue before and I created a post build script that copies all the needed DLLs into my executable's directory.

Something like: copy "$(ProjectDir)Resources\\DLLs\\yourDLL.dll" "$(TargetDir)yourDLL.dll"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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