How to detect the status of msbuild from command line or C# Application

Grant picture Grant · Nov 10, 2011 · Viewed 21.2k times · Source

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)
{
    //
}

Answer

Jim Mischel picture Jim Mischel · Nov 10, 2011

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:

msbuild <args>
if errorlevel 1 goto errorDone

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."