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

时间:2011-02-15 15:48:20

标签: 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 are 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);
        }
    }
}

6 个答案:

答案 0 :(得分:15)

您需要重定向标准输出或标准错误。这是stdout的代码示例:

Process p = new Process();
p.StartInfo.FileName = "hello.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
string stdout = p.StandardOutput.ReadToEnd(); 
p.WaitForExit();

答案 1 :(得分:14)

您必须重定向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.

答案 2 :(得分:4)

您必须检索被调用进程的标准/错误输出。以下是如何做到这一点:

standard output

error output

答案 3 :(得分:2)

假设vsinstr.exe进程将该错误写入标准错误流,您可以通过捕获进程的标准错误流来获取错误消息。为此,您需要将ProcessStartInfo类的RedirectStandardError属性设置为true,将事件处理程序添加到Process类的ErrorDataReceived事件,并在启动vsinstr.exe进程之前调用Process类的BeginErrorReadLine方法。 / p>

以下是一些示例代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

class CaptureProcessOutput
{
    private ManualResetEvent m_processExited = new ManualResetEvent(false);
    private List<string> m_errorMessages = new List<string>();
    private List<string> m_regularMessages = new List<string>();
    private Process m_process = new Process(); 

    public CaptureProcessOutput()
    {
    }

    public void Run (string[] args)
    {
        StringBuilder sb = new StringBuilder("/COVERAGE "); 
        sb.Append("hello.exe"); 
        m_process.StartInfo.FileName = "vsinstr.exe"; 
        m_process.StartInfo.Arguments = sb.ToString(); 
        m_process.StartInfo.UseShellExecute = false;

        m_process.Exited += this.ProcessExited;

        m_process.StartInfo.RedirectStandardError = true;
        m_process.StartInfo.RedirectStandardOutput = true;

        m_process.ErrorDataReceived += this.ErrorDataHandler;
        m_process.OutputDataReceived += this.OutputDataHandler;

        m_process.BeginErrorReadLine();
        m_process.BeginOutputReadLine();

        m_process.Start();

        m_processExited.WaitOne();
    }

    private void ErrorDataHandler(object sender, DataReceivedEventArgs args)
    {
        string message = args.Data;

        if (message.StartsWith("Error"))
        {
            // The vsinstr.exe process reported an error
            m_errorMessages.Add(message);
        }
    }

    private void OutputDataHandler(object sender, DataReceivedEventArgs args)
    {
        string message = args.Data;

        m_regularMessages.Add(message);
    }

    private void ProcessExited(object sender, EventArgs args)
    {
        // This is where you can add some code to be
        // executed before this program exits.
        m_processExited.Set();
    }

    public static void Main (string[] args)
    {
        CaptureProcessOutput cpo = new CaptureProcessOutput();
        cpo.Run(args);
    }
}

答案 4 :(得分:0)

呃 - 在try / catch块里面?我错过了什么吗?

try
{

    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(); 

}
catch(Exception ex)
{
   // Handle exception
}

...请记住,ex.Message可能不包含您在上面指定的代码,而内部异常,例如ex.InnerException可以。

答案 5 :(得分:-1)

我假设您在调试时看到此消息。让我知道这不是你想要的,但你可以使用一个简单的try catch块。

try
{
    //CODE GOES HERE
} catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine(ex.Message);
}