因为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();
Run Code Online (Sandbox Code Playgroud)
当出现错误时,我收到错误消息: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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ath 16
您需要重定向标准输出或标准错误.这是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();
Run Code Online (Sandbox Code Playgroud)
Fel*_*ano 15
您必须重定向StdError并读取流以捕获错误.
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.
Run Code Online (Sandbox Code Playgroud)
假设 vsinstr.exe 进程将该错误写入标准错误流,您可以通过捕获进程的标准错误流来获取错误消息。为此,您需要将 ProcessStartInfo 类的 RedirectStandardError 属性设置为 true,将事件处理程序添加到 Process 类的 ErrorDataReceived 事件,并在启动 vsinstr.exe 进程之前调用 Process 类的 BeginErrorReadLine 方法。
这是一些示例代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)