c# - 如何获取进程的错误信息?

标签 c# process

对于vsinstr -coverage hello.exe,我可以使用如下C#代码。

Process p = new Process(); 
StringBuilder sb = new StringBuilder("/COVERAGE "); 
sb.Append("hello.exe"); 
p.StartInfo.FileName = "vsinstr.exe"; 
p.StartInfo.Arguments = sb.ToString(); 
p.Start(); 
p.WaitForExit();

出现错误时,我收到错误消息:Error VSP1018: VSInstr does not support processing binaries that already instrumented.

如何使用 C# 获取此错误消息?

已解决

我可以从答案中得到错误信息。

using System;
using System.Text;
using System.Diagnostics;

// You must add a reference to Microsoft.VisualStudio.Coverage.Monitor.dll

namespace LvFpga
{
    class Cov2xml
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;         
            p.StartInfo.UseShellExecute = false; 

            StringBuilder sb = new StringBuilder("/COVERAGE ");
            sb.Append("helloclass.exe");
            p.StartInfo.FileName = "vsinstr.exe";
            p.StartInfo.Arguments = sb.ToString();
            p.Start();

            string stdoutx = p.StandardOutput.ReadToEnd();         
            string stderrx = p.StandardError.ReadToEnd();             
            p.WaitForExit();

            Console.WriteLine("Exit code : {0}", p.ExitCode);
            Console.WriteLine("Stdout : {0}", stdoutx);
            Console.WriteLine("Stderr : {0}", stderrx);
        }
    }
}

最佳答案

您必须重定向 StdErr 并读取流以捕获错误。

System.Diagnostics.ProcessStartInfo processStartInfo = 
    new System.Diagnostics.ProcessStartInfo("MyExe.exe", "parameters ...");
int exitCode = 0;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
System.Diagnostics.Process process =
System.Diagnostics.Process.Start(processStartInfo);

process.WaitForExit(); //wait for 20 sec
exitCode = process.ExitCode;
string stdout = process.StandardOutput.ReadToEnd();
string stderr = process.StandardError.ReadToEnd();

// you should see the error string in stdout or stderr depending 
// on how your console applicatione decided to output the failures.

关于c# - 如何获取进程的错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5005874/

相关文章:

c# - 从 Process.StandardOutput 重定向二进制数据会导致数据损坏

android - 应用程序在后台时 Gimbal PlaceEventListener Api 的可用性?

java - 杀死在 Java 中调用的子进程的问题

c# - 使用 GDI+ 和 C# 更改图像对比度

c# - 使用两个通用列表生成交集和分解列表

c# - 处理样本 c#

c# - 每个请求的 Azure 400 错误请求

c# - 单击按钮时如何在文本框中显示不同的sql数据

java - 具有多个 main() 的 JAR——如何从第一个应用程序启动第二个应用程序?

linux - 进程数是否受 RAM 大小的限制?