I am writing up a checkout, build and deployment application in C#, and need to know the best way to detect whether my call to msbuild.exe
has succeeded or not. I have tried to use the error code from the process, but I am not sure whether this is always accurate.
Is there a way (through the code below) that I can tell whether msbuild.exe
completed successfully?
try
{
Process msbProcess = new Process();
msbProcess.StartInfo.FileName = this.MSBuildPath;
msbProcess.StartInfo.Arguments = msbArguments;
msbProcess.Start();
msbProcess.WaitForExit();
if (msbProcess.ExitCode != 0)
{
//
}
else
{
//
}
msbProcess.Close();
}
catch (Exception ex)
{
//
}
As far as I've been able to determine, MSBuild returns an exit code > 0 when it encounters an error. If it doesn't encounter any errors, it returns an exit code of 0. I've never seen it exit with a code < 0.
I use it in a batch file:
In four years of using it this way, I've never had reason to question the correctness of this approach.
Several questions on the MSDN forums ask the same thing. For example: http://social.msdn.microsoft.com/forums/en-US/msbuild/thread/a4ae6b2b-9b1f-4e59-86b4-370f44d73a85. The standard response is, in effect, "if errorlevel is 0, then there was no error."
Sorry if I'm a little bit too late for the party... but nearly 7 years after the question was posted I wanted to see a complete answer for it. I did some tests using the code below, and here are the conclusions:
Analysis
msbuild.exe
returns1
when at least one build error occurs, and returns0
when the build is successfully completed. At present, the program does not take warnings into account, which means a successful build with warnings causesmsbuild.exe
to still return0
.Other errors like: trying to build a project that does not exist, or providing an incorrect argument (like
/myInvalidArgument
), will also causemsbuild.exe
to return1
.Source Code
The following C# code is a complete implementation for building your favorite projects by firing
msbuild.exe
from a command line. Don't forget to setup any necessary environment settings before compiling your projects.Your BuildControl class:
MsBuilder class: Builds stuff by calling MsBuild.exe from command line:
CommandLineProcess class - Starts a command line process and waits until it finishes. All standard output/error is captured, and no separate window is started for the process:
PS: I'm using Visual Studio 2017 / .NET 4.7.2