Search code examples
c#msbuildprocessexit-code

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


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

Solution

  • As far as I've been able to determine, MSBuild returns an exit code greater then zero when it encounters an error. If it doesn't encounter any errors, it returns exit code 0. I've never seen it exit with code lower than 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.

    The standard response is, in effect, "if errorlevel is 0, then there was no error".