Copying a DLL's dependencies in Visual Studio

2019-01-03 09:00发布

How can I set up a project in Visual Studio to copy the third-party DLLs that one of the project's references depends on?

I have a main application project and a class library DLL. The main application references the class library DLL, and the DLL itself references some third-party DLLs. When I compile the main application, it automatically copies the class library DLL to its output directory, but it does not copy the third-party DLLs.

I do not want to add references to the third-party DLLs from the main application project because the main application does not use them, they're only used by the class library.

8条回答
乱世女痞
2楼-- · 2019-01-03 09:33

You can achieve this with the project properties window. Visual Studio allows you to define events to occur, before, or after building. To get to the project properties window simply right-click on your project in the solution explorer window and click on 'properties'. From the left hand side go to the 'build events' tab.

In the post-build box type in a few copy commands. For example:

copy "$(SolutionDir)mydll.dll" "$(TargetDir)"

Where $(SolutionDir) and $(TargetDir) are both predefined variables. The standard syntax is as follows:

copy "source directory and file name" "destination directory"

If you click on the 'edit post build...' button it will bring up a box which has a listing of these predefined variables that you can insert (like $(SolutionDir) and $(TargetDir))

As a side note, this is a useful process for copying other files, such as custom configuration files, images, or any other dependencies your project may have.

查看更多
地球回转人心会变
3楼-- · 2019-01-03 09:35

Take a look at this solution provided by Alex Yakunin http://blog.alexyakunin.com/2009/09/making-msbuild-visual-studio-to.html It worked for me really nicely - the scenario being DevExpress libraries expressly used had other dependencies which caused problems when deployed)

  • Note 1: Visual studio 2010 seems add referenced dlls automatically, however msbuild didn't. So Alex's solution worked since the release scripts used msbuild.
  • Note 2: Also had to make sure that for the referenced libraries (those which were referenced in code) copy-local was actually set to True in the csproj, even though the Solution Explorer said it was. The best way is to set copy-local = False, Save, set copy-local = True, Save.

These two steps - copy-local=true for referenced libraries and adding msbuild targets for the indirect references automated the build setup for me.

查看更多
Viruses.
4楼-- · 2019-01-03 09:39

100% sure this will work. Just replace dll with your personal ref. file

<Reference Include="Xceed.Wpf.Toolkit">
  <HintPath>..\..\..\3rdParty\Extended WPF Toolkit-2.2.1\Xceed.Wpf.Toolkit.dll</HintPath>
   <CopyToOutputDirectory>Always</CopyToOutputDirectory>   
   <SpecificVersion>False</SpecificVersion> 
</Reference>

<Content Include="..\..\..\3rdParty\Extended WPF Toolkit-2.2.1\Xceed.Wpf.Toolkit.dll">
  <Link>Xceed.Wpf.Toolkit.dll</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  <SpecificVersion>False</SpecificVersion>
</Content>
查看更多
老娘就宠你
5楼-- · 2019-01-03 09:41

Go to the main application, references, to your class-library reference.

Set "Copy Local" to True.

It will now copy the bin directory of your class-library into the main application bin directory. Including any sub-dependency third-party dlls.

查看更多
成全新的幸福
6楼-- · 2019-01-03 09:43

The following fragment works for me:

<Project>
  ...
  <ItemGroup>
    <Content Include="Path\to\dll\dllname.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  ...
</Project>

This works for C#. For native C++ it still copy dll to output folder, but this dependency is not visible in Visual Studio, it should be edited in project file directly.

To test on non-trivial example I tried to run C# project A which depends on native C++ project B. B projects depends on thirdparty native dll C - this dependency is implemented via fragment above in project file. When I build A, C is copied to binary folder.

I tried it in Visual Studio 2010.

查看更多
可以哭但决不认输i
7楼-- · 2019-01-03 09:44

I would not recommend doing this. You end up with an N^2 explosion in the number of assemblies being copied around (and potentially, being rebuilt). If possible, you should have all of your projects place their assemblies in the same $(OutDir). If you're using TFS, Team Build does this for you.

查看更多
登录 后发表回答