如何使用 C# 获取错误消息 [英] How to get the error message with C#
本文介绍了如何使用 C# 获取错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于vsinstr -coverage hello.exe
,我可以使用C#代码如下.
For vsinstr -coverage hello.exe
, I can use the C# code as follows.
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 不支持处理已检测的二进制文件.
.
When there's an error, I get the error message : Error VSP1018: VSInstr does not support processing binaries that are already instrumented.
.
如何使用 C# 获取此错误消息?
How can I get this error message with C#?
我可以从答案中得到错误信息.
I could get the error messages from the answers.
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);
}
}
}
推荐答案
您需要重定向标准输出或标准错误.这是 stdout 的代码示例:
You need to redirect the standard output or standard error. Here's a code sample for 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();
这篇关于如何使用 C# 获取错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文