如何将必要的文件包含到独立客户端项目的输出中?

我的解决方案包括:

Client – 启动项目,UI层。 取决于: App

App – 库,应用程序层,汇编程序。 取决于: Lib1 ,……

Lib1 – 库,业务逻辑层。 需要特定文件才能正常工作: ThirdParty.dll

我已将ThirdParty.dll添加到Lib1项目( Add > Existing Item... > Add ),并将dll文件的“ Copy to Output Directory属性设置为Copy Always 。 现在,dll文件被复制到Lib1输出和App输出,但不会复制到我需要的Client输出。

在每个解决方案构建中将ThirdParty.dll复制到Client的输出的正确(简单?明显?)方法是什么?

UPD ThirdParty.dll不是引用。 实际上,这是另一个参考依赖。 我的问题适用于需要在运行应用程序的文件夹中的任何文件。

录制的video,以确保我做得对:http: //youtu.be/QwS2tOIc5yQ

将现有项添加为链接:

我在VS2010遇到了类似的问题,我最终将文件添加为Link并将其属性更新为Copy Always

在您的情况下,在CLIENT项目中,将ThirdParty.dll添加为Link (添加>现有项>添加为链接),并将dll文件的“ Copy to Output Directory属性设置为Copy Always注意 :它将从项目节点复制文件夹层次结构。

仅供参考:我实际上使用的是开源LibGit2Sharp ,它需要在输出目录中提供dll(libGit2.dll)。 因此,在UI层中,添加了包含LibGit2Sharp.dll应用层作为参考; 我不得不将libGit2.dll添加为Link + Copy Always 。 这是推荐的解决方案。

后期制作:

另一种选择可能是为CLIENT编写一个后期构建脚本

要了解MSBuild上的Copy是如何工作的,可以参考Microsoft.Common.targets文件(应该可以在系统中找到@ C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Microsoft.Common.targets)

  

它很容易,而且很奇怪,它不适合你。 我刚检查过这个有效:

  1. ThirdParty.dll添加到Lib项目。
  2. Build Action应该设置为Content (默认值)!!!
  3. Copy to Output Directory设置Copy to Output DirectoryCopy if newer Copy alwaysCopy always
  4. 来自App Reference Lib
  5. Client参考App

这适用于Visual Studio 2012,但似乎在Visual Studio 2010中不起作用。

VS2010:

 Target "AssignTargetPaths" skipped. Previously built successfully. Target "_SplitProjectReferencesByFileExistence" in file "C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "Test.ContentFiles\App\App.csproj" (target "GetCopyToOutputDirectoryItems" depends on it): Task "ResolveNonMSBuildProjectOutput" skipped, due to false condition; ('$(BuildingInsideVisualStudio)'=='true' and '@(ProjectReferenceWithConfiguration)'!='') was evaluated as ('true'=='true' and ''!=''). Done building target "_SplitProjectReferencesByFileExistence" in project "App.csproj". Target "GetCopyToOutputDirectoryXamlAppDefs" in file "C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Xaml.targets" from project "Test.ContentFiles\App\App.csproj" (target "GetCopyToOutputDirectoryItems" depends on it): Task "AssignTargetPath" Done executing task "AssignTargetPath". Done building target "GetCopyToOutputDirectoryXamlAppDefs" in project "App.csproj". Target "GetCopyToOutputDirectoryItems" in file "C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "C:\Users\Ark-kum\Documents\visual studio 2010\Projects\Test.ContentFiles\App\App.csproj" (entry point): Task "MSBuild" skipped, due to false condition; ('@(_MSBuildProjectReferenceExistent)' != '' and '$(_GetChildProjectCopyToOutputDirectoryItems)' == 'true' and '%(_MSBuildProjectReferenceExistent.Private)' != 'false' and '$(UseCommonOutputDirectory)' != 'true') was evaluated as ('' != '' and 'true' == 'true' and '' != 'false' and 'false' != 'true'). Task "AssignTargetPath" Done executing task "AssignTargetPath". Done building target "GetCopyToOutputDirectoryItems" in project "App.csproj". Done executing task "MSBuild". Task "AssignTargetPath" Done executing task "AssignTargetPath". Done building target "GetCopyToOutputDirectoryItems" in project "Client.csproj". Target "_CopyOutOfDateSourceItemsToOutputDirectory" in file "C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "Test.ContentFiles\Client\Client.csproj" (target "_CopySourceItemsToOutputDirectory" depends on it): Building target "_CopyOutOfDateSourceItemsToOutputDirectory" completely. Output file "bin\Debug\FileList.App.txt" does not exist. Task "Copy" Copying file from "Test.ContentFiles\App\FileList.App.txt" to "bin\Debug\FileList.App.txt". 

VS2012:

 Target "GetCopyToOutputDirectoryItems" skipped. Previously built successfully. Done executing task "MSBuild". Task "AssignTargetPath" Done executing task "AssignTargetPath". Done building target "GetCopyToOutputDirectoryItems" in project "Client.csproj". Target "_CopyOutOfDateSourceItemsToOutputDirectory" in file "C:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets" from project "Test.ContentFiles\Client\Client.csproj" (target "_CopySourceItemsToOutputDirectory" depends on it): Building target "_CopyOutOfDateSourceItemsToOutputDirectory" completely. Output file "bin\Debug\FileList.Lib.txt" does not exist. Output file "bin\Debug\FileList.App.txt" does not exist. Task "Copy" Copying file from "Test.ContentFiles\Lib\FileList.Lib.txt" to "bin\Debug\FileList.Lib.txt". Copying file from "Test.ContentFiles\App\FileList.App.txt" to "bin\Debug\FileList.App.txt". 

从.target文件引用:

 ============================================================ GetCopyToOutputDirectoryItems Get all project items that may need to be transferred to the output directory. This includes baggage items from transitively referenced projects. It would appear that this target computes full transitive closure of content items for all referenced projects; however that is not the case. It only collects the content items from its immediate children and not children of children. The reason this happens is that the ProjectReferenceWithConfiguration list that is consumed by _SplitProjectReferencesByFileExistence is only populated in the current project and is empty in the children. The empty list causes _MSBuildProjectReferenceExistent to be empty and terminates the recursion. ============================================================ 

您可以使用(在visual studio中)Lib1项目属性 – >构建事件 – >构建后事件命令行。

在“生成后事件命令行”编辑框中键入复制命令,该命令将所需文件复制到客户端输出文件夹。 在该编辑框中,您可以使用各种VS宏(单击“编辑后期构建” – >宏),如下所示:

 copy path\to\ThirdParty.dll $(SolutionDir)Client\bin\Debug 

也许,您需要在第一时间“构建” – >“重建解决方案”。