MsBuild copy file after build

2020-02-09 06:55发布

I want to copy an xml file from the main directory to bin\Debug after building the project, but my solution doesn't work. I edited .csproj file and added:

<Target Name="AfterBuild">
     <Copy SourceFiles="Controllers.xml" DestinationFolder="\bin\Debug" ContinueOnError="true" />
</Target>

What am I doing wrong? The build is successful.

3条回答
在下西门庆
2楼-- · 2020-02-09 07:08

Your destination folder is (most likely) wrong. If you specify it with a leading backslash, it is actually just a shortform for <current-drive-letter>\bin\Debug (making it effectively an absolute path, like C:\bin\Debug).

Either use bin\Debug, or better yet use the OutputPath variable, which is set to either bin\Debug or bin\Release depending on your build configuration.

Example:

<Target Name="AfterBuild">
     <Copy SourceFiles="Controllers.xml" DestinationFolder="$(OutputPath)" ContinueOnError="true" />
</Target>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-09 07:09

You have to specify the full path. I suspect the MsBuild copy task is running from the "Default path" of Visual Studio, and the file can not be found. Also, you most likely want the file to end up in the build target directory.

<Target Name="AfterBuild">
    <Copy SourceFiles="$(ProjectDir)Controllers.xml" DestinationFolder="$(TargetDir)" ContinueOnError="true" />
</Target>
查看更多
地球回转人心会变
4楼-- · 2020-02-09 07:23

Is the xml file in your project? Then one of its properties is CopyToOutputDirectory. Set it to CopyAlways and when the project builds the file will be copied out to bin\debug.

查看更多
登录 后发表回答