visual-studio Visual Studio 生成后事件 - 复制到相对目录位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/834270/
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
Visual Studio Post Build Event - Copy to Relative Directory Location
提问by Preets
On a successful build, I wish to copy the contents of the output directory to a different location under the same "base"folder. This parent folder is a relative part and can vary based on Source Control settings.
成功构建后,我希望将输出目录的内容复制到同一“基本”文件夹下的不同位置。此父文件夹是一个相对部分,可以根据源代码管理设置而有所不同。
I have listed a few of the Macro values available to me ...
我列出了一些可供我使用的宏值......
$(SolutionDir) = D:\GlobalDir\Version\AppName\Solution1\build
$(SolutionDir) = D:\GlobalDir\Version\AppName\Solution1\build
$(ProjectDir) = D:\GlobalDir\Version\AppName\Solution1\Version\ProjectA\
$(ProjectDir) = D:\GlobalDir\Version\AppName\Solution1\Version\ProjectA\
I want to copy the Output Dir contents to the following folder :
我想将输出目录内容复制到以下文件夹:
D:\GlobalDir\Version\AppName\Solution2\Project\Dependency
D:\GlobalDir\Version\AppName\Solution2\Project\Dependency
The base location "D:\GlobalDir\Version\AppName" needs to be fetched from one of the above macros. However, none of the macro values list onlythe parent location.
需要从上述宏之一中获取基本位置“D:\GlobalDir\Version\AppName”。但是,没有任何宏值仅列出父位置。
How do I extract onlythe base location for the post build copy command ?
如何仅提取构建后复制命令的基本位置?
采纳答案by gbjbaanb
If none of the TargetDir or other macros point to the right place, use the ".." directory to go backwards up the folder hierarchy.
如果 TargetDir 或其他宏都没有指向正确的位置,请使用“..”目录向后移动文件夹层次结构。
ie. Use $(SolutionDir)\..\..to get your base directory.
IE。使用$(SolutionDir)\..\..让您的基本目录。
For list of all macros, see here:
有关所有宏的列表,请参见此处:
回答by Lucas B
Here is what you want to put in the project's Post-build event command line:
以下是您要放入项目的构建后事件命令行中的内容:
copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)lib$(ProjectName).dll"
EDIT: Or if your target name is different than the Project Name.
编辑:或者如果您的目标名称与项目名称不同。
copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)lib$(TargetName).dll"
回答by ichiban
You could try:
你可以试试:
$(SolutionDir)..\..\
回答by drzaus
I think this is related, but I had a problem when building directly using msbuildcommand line (from a batch file) vs building from within VS.
我认为这是相关的,但是在直接使用msbuild命令行(从批处理文件)与从 VS 内部构建时我遇到了问题。
Using something like the following:
使用类似以下内容:
<PostBuildEvent>
MOVE /Y "$(TargetDir)something.file1" "$(ProjectDir)something.file1"
start XCOPY /Y /R "$(SolutionDir)SomeConsoleApp\bin$(ConfigurationName)\*" "$(ProjectDir)App_Data\Consoles\SomeConsoleApp\"
</PostBuildEvent>
(note: start XCOPYrather than XCOPYused to get around a permissions issue which prevented copying)
(注意:start XCOPY而不是XCOPY用来解决阻止复制的权限问题)
The macro $(SolutionDir)evaluated to ..\when executing msbuild from a batchfile, which resulted in the XCOPYcommand failing. It otherwise worked fine when built from within Visual Studio. Confirmed using /verbosity:diagnosticto see the evaluated output.
从批处理文件执行 msbuild 时,宏$(SolutionDir)计算为..\,这导致XCOPY命令失败。从 Visual Studio 中构建时,它可以正常工作。确认/verbosity:diagnostic用于查看评估输出。
Using the macro $(ProjectDir)..\instead, which amounts to the same thing, worked fine and retained the full path in both build scenarios.
$(ProjectDir)..\改用宏,这相当于同一件事,效果很好,并在两种构建方案中都保留了完整路径。
回答by Rihan Meij
Would it not make sense to use msbuild directly? If you are doing this with every build, then you can add a msbuild task at the end? If you would just like to see if you can't find another macro value that is not showed on the Visual Studio IDE, you could switch on the msbuild options to diagnostic and that will show you all of the variables that you could use, as well as their current value.
直接使用 msbuild 没有意义吗?如果每次构建都这样做,那么您可以在最后添加一个 msbuild 任务吗?如果您只想查看是否找不到 Visual Studio IDE 上未显示的另一个宏值,您可以打开 msbuild 选项以进行诊断,这将显示您可以使用的所有变量,如以及它们的当前值。
To switch this on in visual studio, go to Tools/Options then scroll down the tree view to the section called Projects and Solutions, expand that and click on Build and Run, at the right their is a drop down that specify the build output verbosity, setting that to diagnostic, will show you what other macro values you could use.
要在 Visual Studio 中打开此功能,请转到“工具/选项”,然后将树视图向下滚动到名为“项目和解决方案”的部分,展开该部分并单击“构建和运行”,右侧是一个下拉菜单,指定构建输出详细程度,将其设置为诊断,将显示您可以使用的其他宏值。
Because I don't quite know to what level you would like to go, and how complex you want your build to be, this might give you some idea. I have recently been doing build scripts, that even execute SQL code as part of the build. If you would like some more help or even some sample build scripts, let me know, but if it is just a small process you want to run at the end of the build, the perhaps going the full msbuild script is a bit of over kill.
因为我不太清楚您想要达到什么级别,以及您希望构建的复杂程度,这可能会给您一些想法。我最近一直在做构建脚本,甚至在构建过程中执行 SQL 代码。如果您需要更多帮助或什至是一些示例构建脚本,请告诉我,但如果这只是您想在构建结束时运行的一个小过程,那么运行完整的 msbuild 脚本可能有点过头了.
Hope it helps Rihan
希望对日韩有帮助

