Click here to Skip to main content
13,894,020 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 29 Jan 2009
Licenced CPOL

Suppressing Post-build Events When Using MSBuild

, 29 Jan 2009
Rate this:
Please Sign up or sign in to vote.
Supress a post-build event when building using MSBuild.


Working on a large application can uncover some unique issues. Learning how everything is organized, built, referenced and such - can be challenging to say the least. In my experience with such an application, I came across an issue with using oost-build events, but only when using MSBuild. Because of the way the applications were built and organized, I had to add a post-build event to a particular project to copy the target “DLL” to a directory further up the folder hierarchy. In my case - when adding a Unit Test - you had to reference the “project under test” from one of the top-level directories - instead of from the “bin/Debug” folder. This was because the “Unit Test” project was referenced by many different Visual Studio solutions. Unfortunately, this also meant that changes to the project under test would not be seen by the Unit Test, until the newly built “DLL” was copied to the top-level folder where it is referenced by the Unit Test.

It’s a simple enough problem to fix; adding a simple “copy” command to the “Post-build event command” on the project under test does the trick:

copy /y $(TargetFileName) "..\..\..\..\Binaries\" 

This works as expected when you build a solution or project from within Visual Studio. However, it broke the full application build. To understand why, you need to know that whatever you put in the “Post-build event command” textbox basically gets put into a batch file - and executed from the project’s “bin/Debug” folder (or bin/Release, depending on your configuration).

You can see that the command listed above will copy the “TargetFileName” into a “Binaries” folder that is located four folders above the current directory. However, when we run the MSBuild build, it is executed from a different directory… which means that all of the post-build events that exist in any of the projects being built will be executed from that directory as well.

This causes an error in the MSBuild output because the Post-build event command cannot find the “Binaries” folder in the specified location.

However, we can workaround this problem, and essentially ignore the post-build event while running MSBuild. Remember that the Post-build event command is really just a place to put DOS commands that get executed after a successful build. This means that we can use some DOS variables. First, if you’re not starting the MSBuild process using a batch file already, create a batch file that calls the MSBuild process. Then, add a variable in the batch file before you call the MSBuild process. Something like this:

msbuild TFSBuild.proj >> BuildOutput.txt

Then, you need to modify your post-build event to only do something if the variable is not set, or has the wrong value:

IF NOT '%ISFULLBUILD%'=='True' copy /y $(TargetFileName) "..\..\..\..\Binaries\"

That’s it! The TargetFileName (Whatever.dll) will only get copied when you’re building from within Visual Studio. But, when you build using your new batch file - it will not execute the command after the IF statement.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Leftend
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAbsolute paths Pin
Dmitri Nеstеruk20-Jun-11 3:35
memberDmitri Nеstеruk20-Jun-11 3:35 
GeneralMy vote of 5 Pin
smkowalczyk26-Oct-10 10:33
membersmkowalczyk26-Oct-10 10:33 
GeneralProject references Pin
Paul B.3-Feb-09 12:06
memberPaul B.3-Feb-09 12:06 
GeneralRe: Project references Pin
Leftend3-Feb-09 12:25
memberLeftend3-Feb-09 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Leftend
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid