Visual Studio生成后事件-如果文件位于子文件夹中,如何将文件复制到输出(bin)目录(仅适用于更新的目录) [英] Visual Studio Post-Build event - How to copy files to output (bin) directory (only if newer) if file(s) are in a subfolder
问题描述
如何将所需的二进制文件(例如dll,图标,工具,doc)复制到bin文件夹中的目标目录中?
How to copy needed binary files (e.g. dlls, icons, tools, doc) into the target directory in the bin folder?
有一个明显的集成可能性:包括对源文件进行处理的文件,并在复制到输出目录"中的文件属性中将其标记为始终复制"或复制较新".
There is an obvious integrated possibility: Including the file as been done with source files and mark in the file properties in "Copy to output directory" as "Copy always" or "Copy newer".
但这仅在二进制文件保留在主项目文件夹中的情况下有效.如果整理一下,然后将这些文件放在一个子文件夹中以与源文件区分开,则该子文件夹将在目标目录1:1中创建,而资源文件不在目标根目录中.
But this works only, if that binary files stay included in the main project folder. If you tidy up, and put those files in a subfolder just to differentiate from source files, that subfolder is created in the target directory 1:1, and the resource files are not in the root target dir.
相关问题: VS PostBuild事件-复制文件(如果存在)
推荐答案
回答我自己的问题以分享我的知识.
Answering my own question to share my knowledge.
鉴于您要将所有文件复制到VS项目的子文件夹"RuntimeEnv"中:
在任何项目文件的构建属性下,都有一个构建事件"选项卡,其中包含构建前和构建后命令行的可能性,可以在不设置TFS构建定义的情况下包括此类副本或相似的构建要求.
Under the build properties of any project file there is a tab "Build events" with Pre-build and Post-build commandline possibilities to include such copy or similar build requirements without setting up a TFS build definition.
您可以在其中包含类似批处理的命令,并可以访问一些预构建变量.例如,请参阅问题中的链接.
There you could include batch like commands and have access to some prebuild variables. See for example the link in the question.
复制没有提供足够的功能.在下文中,我提到了xcopy和robocopy的解决方案
copy doesn´t provide enough features. In the following, I mention a solution for xcopy and robocopy
由于我更喜欢robocopy.exe,因为它非常快速,可靠并且是仅复制较新文件(/XO)的内置选项,因此我使用以下命令代替了简单的"copy"命令:
As I prefer robocopy.exe as very fast, reliable and a build-in option to copy only newer files (/XO), I use the following instead of a simple "copy" command :
robocopy.exe /XO "$(ProjectDir)RuntimeEnv" "$(TargetDir)." >nul 2>&1
exit 0
robocopy的两个特长是在这里解决的:
There are two specialties of robocopy worked around here:
-
."在"$(TargetDir)"之后是必需的,因为robocopy 不喜欢以反斜杠结尾的路径,在这种情况下就是这样.
The "." after the "$(TargetDir)" is necessary because robocopy doesn´t like paths ending with backslash which is the case here.
最后一行是必需的,因为robocopy会以返回码退出 包含复制的文件数,因此"0"与 按预期成功.
The last line is necessary because robocopy exits with a returncode containing the number of files copied, so "0" is not the same as successful as expected.
可以添加一些MSBuild目标行来更改预期的返回码,但是在Post-Build事件中执行操作最简单.
It would be possible to add some MSBuild target lines to change the expected returncode, but doing it in the Post-Build event is most simple.
HTH.
P.S.正如Hans Passant在评论中指出的那样,xcopy /D
是一个更简单的解决方案.
它是-并且如果robocopy的其他属性或参数都没有用,则可能更可取.
P.S. As Hans Passant pointed out in a comment, xcopy /D
is an easier solution.
It is- and if no other properties or parameters of robocopy are useful, this could be preferred.
这将使这一行成行:
xcopy.exe /D /Y "$(ProjectDir)RuntimeEnv\*.*" "$(TargetDir)"
xcopy的输出行不会正常打扰,并提示构建后的情况-否则您也可以添加>nul 2>&1
.
The output lines of xcopy would not disturb normally, and give a hint what is happening at post-build - otherwise you could add >nul 2>&1
too.
这篇关于Visual Studio生成后事件-如果文件位于子文件夹中,如何将文件复制到输出(bin)目录(仅适用于更新的目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!