应用程序检测msbuild的状态

应用程序检测msbuild的状态

本文介绍了如何从命令行或C#应用程序检测msbuild的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写一个签出,构建和部署应用程序,并且需要了解检测我对msbuild.exe的调用是否成功的最佳方法.我尝试使用该过程中的错误代码,但不确定是否总是正确的.

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.

是否有一种方法(通过下面的代码)可以告诉msbuild.exe是否成功完成?

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

推荐答案

据我所知,MSBuild遇到错误时将返回退出代码> 0.如果没有遇到任何错误,它将返回退出代码0. 0.

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.

MSDN论坛上的几个问题都问同样的事情.例如: http://social.msdn.microsoft.com/forums/zh-CN/msbuild/thread/a4ae6b2b-9b1f-4e59-86b4-370f44d73a85 .实际上,标准响应是如果错误级别为0,则没有错误."

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

这篇关于如何从命令行或C#应用程序检测msbuild的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:39