问题描述
我正在用 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 的代码退出.
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.
MSDN 论坛上的几个问题都问同样的问题.
实际上,标准响应是如果错误级别为 0,则没有错误".
The standard response is, in effect, "if errorlevel is 0, then there was no error".
这篇关于如何从命令行或 C# 应用程序检测 msbuild 的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!