visual-studio 在 Visual Studio 中复制 DLL 的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:42:31  来源:igfitidea点击:

Copying a DLL's dependencies in Visual Studio

visual-studiodll

提问by M. Dudley

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?

如何在 Visual Studio 中设置项目以复制项目引用之一所依赖的第三方 DLL?

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.

我有一个主应用程序项目和一个类库 DLL。主应用程序引用类库DLL,DLL本身引用了一些第三方DLL。当我编译主应用程序时,它会自动将类库 DLL 复制到其输出目录,但不会复制第三方 DLL。

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.

我不想从主应用程序项目中添加对第三方 DLL 的引用,因为主应用程序不使用它们,它们仅由类库使用。

采纳答案by Brian

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.

您可以使用项目属性窗口实现此目的。Visual Studio 允许您定义在构建之前或之后发生的事件。要进入项目属性窗口,只需在解决方案资源管理器窗口中右键单击您的项目,然后单击“属性”。从左侧转到“构建事件”选项卡。

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:

其中$(SolutionDir)$(TargetDir)都是预定义的变量。标准语法如下:

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

如果您单击“编辑后构建...”按钮,它将弹出一个框,其中包含您可以插入的这些预定义变量的列表(如$(SolutionDir)$(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.

附带说明一下,这是复制其他文件(例如自定义配置文件、图像或您的项目可能具有的任何其他依赖项)的有用过程。

回答by sergtk

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.

这适用于 C#。对于本机 C++,它仍将 dll 复制到输出文件夹,但此依赖项在 Visual Studio 中不可见,应直接在项目文件中进行编辑。

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.

为了测试重要的示例,我尝试运行依赖于本机 C++ 项目 B 的 C# 项目 A。B 项目依赖于第三方本机 dll C - 此依赖项是通过项目文件中的上述片段实现的。当我构建 A 时,C 被复制到二进制文件夹。

I tried it in Visual Studio 2010.

我在 Visual Studio 2010 中尝试过。

回答by Anuroopa Shenoy

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

看看 Alex Yakunin 提供的这个解决方案 http://blog.alexyakunin.com/2009/09/making-msbuild-visual-studio-to.html它对我很有用 - 明确使用 DevExpress 库的场景有部署时导致问题的其他依赖项)

  • 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.
  • 注 1:Visual Studio 2010 似乎会自动添加引用的 dll,但 msbuild 没有。因此,由于发布脚本使用了 msbuild,Alex 的解决方案有效。
  • 注意 2:还必须确保对于引用的库(在代码中引用的库),即使解决方案资源管理器说它是,在 csproj 中 copy-local 实际上设置为 True。最好的方法是设置copy-local = False, Save,设置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.

这两个步骤 - 引用库的 copy-local=true 和为间接引用添加 msbuild 目标为我自动化了构建设置。

回答by Curtis Yallop

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

转到主应用程序,参考,到您的类库参考。

Set "Copy Local" to True.

将“复制本地”设置为 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.

它现在会将类库的 bin 目录复制到主应用程序 bin 目录中。包括任何子依赖第三方 dll。

回答by MrCalvin

I don't like to have my dependency files located in the project root folder but in a subfolder. But the files has to be placed in the root folder in the build folder.

我不希望我的依赖文件位于项目根文件夹中,而是位于子文件夹中。但是这些文件必须放在 build 文件夹的根文件夹中。

My build events look like this:

我的构建事件如下所示:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

 Command: call xcopy /S /Y "$(SolutionDir)Dependencies\*.*" "$(TargetDir)"

In case "Dependencies" also contains subfolders, as mine does.

如果“依赖项”也包含子文件夹,就像我的一样。

回答by Richard Berg

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.

我不建议这样做。最终,被复制(并可能被重建)的程序集数量激增 N^2。如果可能,您应该让所有项目都将它们的程序集放在同一个 $(OutDir) 中。如果您使用 TFS,Team Build 会为您执行此操作。

回答by jean-maurice Destraz

If you want to copy new file only in post-build, you can also use xcopywith flags /i /d /y

如果您只想在后期构建中复制新文件,您还可以使用带有标志/i /d /y 的xcopy

xcopy "$(ProjectDir)SubDir\*.dll" "$(TargetDir)" /i /d /y

回答by Petar

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

100% 肯定这会奏效。只需将 dll 替换为您的个人参考即可。文件

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

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

回答by James McConnell

I'm not really aware of any way to do this other than adding a reference to said .dll in the project itself. We've run across this in some of our own projects here, and the only solution we've found is to add the reference. I want to say that one of our developers did some research and found this to be the only solution, but don't quote me on that.

除了在项目本身中添加对上述 .dll 的引用之外,我真的不知道有什么方法可以做到这一点。我们已经在我们自己的一些项目中遇到了这个问题,我们找到的唯一解决方案是添加引用。我想说我们的一位开发人员做了一些研究,发现这是唯一的解决方案,但不要引用我的话。