如何从命令行或 C# 应用程序检测 msbuild 的状态

How to detect the status of msbuild from command line or C# Application(如何从命令行或 C# 应用程序检测 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 的代码退出.

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 的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)