简体   繁体   中英

DLL lib in a different folder of EXE (relative path)

I've got a windows application project that builds in an exe that uses a DLL (RestSharp). The project is in Visual Studio 2008. I've added the DLL in the References and if I click on the DLL i see:

  • aliases: global
  • copy local: true
  • specific version: false
  • Path: C:\Dev\MyProject\Lib\Restsharp.dll

With these settings when I build my exe, the DLL lib is copied in the build dir. On my deployment environment I'd like to have my exe in a folder and the DLL in a different one Example: C:\Test\Exe (EXE FOLDER) C:\Test\Lib (DLL FOLDER)

Is that possible? How?

I've set C:\Test\Lib in my Reference Paths, but that's an absolute path, I was looking for a relative one. Also the DLL path in References is absolute. What am I doing wrong? I'd like the exe to "find" the DLL no matter where it is copied (C:\Test or D:\Test or just D:)

Thank you

Better you register that DLL in GAC, one DLL from same path will be using, Please refer below to know https://www.c-sharpcorner.com/UploadFile/dacca2/register-your-assembly-in-gac-using-gacutil-exe/

Before starting, take a deep breath and determine exactly why you want this, because often this complicates things without much gain.

You can do this by using assembly probing.

First, its a good thing to read this firts: https://docs.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies

somewhere in the article is this:

The privatePath attribute of the element, which is the user-defined list of subdirectories under the root location. This location can be specified in the application configuration file and in managed code using the AppDomainSetup.PrivateBinPath property for an application domain.

And we can see how its done in the docs of the probing tag; https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/probing-element

Add an app.config file to your project and edit it like so:


<configuration>  
   <runtime>  
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
         <probing privatePath="bin\REEE;someTestPath"/>  
      </assemblyBinding>  
   </runtime>  
</configuration>  

Now, when the dll is added as a reference, its automatically copied to your output folder. We however want the dll to be in the subfolder instead. There're two ways you can do this that i know of;

  1. Copy the dll over from the output directory to your subpath. You can use a post-build task and eg xcopy to do this (google around this shouldn't be hard). You could also use msbuild tasks to copy the dll
  2. Add the dll as content to a samely named subfolder in the csproj, set to copy local. See the picture below. 在此处输入图像描述

Also check this relating answer:

https://stackoverflow.com/a/58124935/4122889

I highly suggest you don't do this unless you have a very good reason to do so. And at that point i'm curious as to what that reason is:)

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