Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the error message with C#

Tags:

c#

process

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

When there's an error, I get the error message : Error VSP1018: VSInstr does not support processing binaries that are already instrumented..

How can I get this error message with C#?

SOLVED

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);         }     } } 
like image 699
prosseek Avatar asked Feb 15 '11 15:02

prosseek


People also ask

What is error message in C?

Error numbers in C language refer to the list of constant integer values or error codes defined by the ISO C, POSIX, and the C++ standard libraries. The error codes represent different types of errors. When a function fails in C, it returns -1 or NULL , and the global variable errno (defined in the errno.

Does C have error handling?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

How does errno work in C?

Global Variable errno: When a function is called in C, a variable named as errno is automatically assigned a code (value) which can be used to identify the type of error that has been encountered. Its a global variable indicating the error occurred during any function call and defined in the header file errno. h.

What does exit () do in C?

In the C Programming Language, the exit function calls all functions registered with atexit and terminates the program. File buffers are flushed, streams are closed, and temporary files are deleted.


2 Answers

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(); 
like image 181
Mark Heath Avatar answered Oct 06 '22 21:10

Mark Heath


You have to redirect StdError and read the stream to catch the errors.

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. 
like image 20
Felice Pollano Avatar answered Oct 06 '22 21:10

Felice Pollano