.net MSbuild 复制整个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5123655/
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-09-03 15:19:34  来源:igfitidea点击:

MSbuild Copy whole folder

.netvisual-studiomsbuild

提问by Sonic Soul

trying to copy a whole folder, but when i do this:

试图复制整个文件夹,但是当我这样做时:

<Copy SourceFiles="$(TargetDir)\*.*" DestinationFolder="$(BuildOutput)\SomeDir" />

the copy attempts to do this: copy c:\source\*.* c:\destination\SomeDir\*.*and fails with "illegal characters"

副本尝试这样做:复制 c:\source\*.* c:\destination\SomeDir\*.*并以“非法字符”失败

回答by Brian Kretzler

Specify your ItemGroup for SourceFiles explicitly.

明确指定 SourceFiles 的 ItemGroup。

<ItemGroup>
    <_CopyItems Include="$(TargetDir)\*.*" />
</ItemGroup>
<Copy
    SourceFiles="@(_CopyItems)"
    DestinationFolder="$(BuildOutput)\SomeDir"
    />

Note that _CopyItems is an item type, so it's referenced using '@' symbol rather than '$'.

请注意,_CopyItems 是一种项目类型,因此使用“@”符号而不是“$”来引用它。

回答by Siarhei Kuchuk

Copying files can be done with the following code snippet which handles antivirus programs and subdirectories

可以使用以下处理防病毒程序和子目录的代码片段来复制文件

  <ItemGroup>
        <SomeAppStuff Include="$(SolutionDir)\ProjectXXX\bins\**\*.*" />
  </ItemGroup>
  <Copy 
      SourceFiles="@(SomeAppStaff)" 
      DestinationFolder="$(OutputPath)\%(RecursiveDir)" 
      SkipUnchangedFiles="true"
      OverwriteReadOnlyFiles="true" 
      Retries="3"
      RetryDelayMilliseconds="300"/>

Specifying $(OutputPath)\%(RecursiveDir)will ask Copy task to respect subfolders, so it will place subfolders of source directory to subfolders of target directories.

指定$(OutputPath)\%(RecursiveDir)将要求复制任务尊重子文件夹,因此它将源目录的子文件夹放置到目标目录的子文件夹中。

SkipUnchangedFileswill increase build speed on computers with enough memory, because Windows optimizes IO for frequently used files when there's enough RAM.

SkipUnchangedFiles将在具有足够内存的计算机上提高构建速度,因为当有足够的 RAM 时,Windows 会优化常用文件的 IO。

Retriesand RetryDelayMillisecondshandles issues related a) Compressed NTFS file system, when builds fails in seldom cases b) Antivirus Software with SSD drives.

RetriesRetryDelayMilliseconds处理与 a) 压缩 NTFS 文件系统相关的问题,在极少数情况下构建失败 b) 带有 SSD 驱动器的防病毒软件。

回答by Terence

If you put the folder in the root of your c# project then you can simple put this in your csproj.

如果您将该文件夹放在 c# 项目的根目录中,那么您可以简单地将它放在您的 csproj 中。

<ItemGroup>
    <None Update="FolderToCopy\**\*.*">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
</ItemGroup>

I have only tested in the 2017 version of csproj, but I assume it's backwards compatible. Could be wrong on that though

我只在 2017 版的 csproj 中进行了测试,但我认为它是向后兼容的。虽然可能是错的

回答by RichTea

Looking at the MSDN documentation, I believe the SourceFiles parameter requires an ITaskItem[] value. See MSDN MSBuild Copy Task

查看 MSDN 文档,我相信 SourceFiles 参数需要 ITaskItem[] 值。请参阅MSDN MSBuild 复制任务

The last example on the above link is to do a recursive copy from one directory to another, maintaining the folder structure.

上面链接的最后一个例子是从一个目录到另一个目录进行递归复制,保持文件夹结构。

回答by Arthur

Succeeded to accomplish this task like this

成功完成了这样的任务

<Target Name="AfterBuild">
  <ItemGroup>
    <SomeDir Include="$(SolutionDir)\SomeOtherProject\SomeDir\**\*" />
  </ItemGroup>
  <Copy 
    SourceFiles="@(SomeDir)" 
    DestinationFiles="@(SomeDir->'$(OutDir)\SomeDir\%(RecursiveDir)%(Filename)%(Extension)')" 
    SkipUnchangedFiles="true" 
    OverwriteReadOnlyFiles="true" 
    Retries="3" 
    RetryDelayMilliseconds="300" />

回答by Ricardo Rodrigues

For me what worked was this: - kept folder structure - copied all files within the folder - works for any folder, doesn't have to be in the project or the same project folder

对我来说,有效的是: - 保留文件夹结构 - 复制文件夹中的所有文件 - 适用于任何文件夹,不必在项目或同一个项目文件夹中

<ItemGroup>
    <_CopyItems Include="<path relative to project>\**\*.*" />
</ItemGroup>

<Target Name="AfterBuild">
  <Copy SourceFiles="@(_CopyItems)" DestinationFiles="@(_CopyItems->'$(OutDir)\<output folder>\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>

legend:

传奇:

  • <path relative to project>: this could be any path, using ..\ for going above the proj folder works
  • <output folder>: folder you want the whole file structure to be dropped into, excluding the source folder.
  • $(OutDir)will be bin\Debug or whatever build mode you have, if you want something else, change that.
  • <path relative to project>:这可以是任何路径,使用 ..\ 在 proj 文件夹上方工作
  • <output folder>: 要将整个文件结构放入的文件夹,不包括源文件夹。
  • $(OutDir)将是 bin\Debug 或您拥有的任何构建模式,如果您想要其他东西,请更改它。