13

I'm trying to Start command promt process with args. Now I want to obtain information about errors if they exist.

someProcess = System.Diagnostics.Process.Start(cmd, someArgs);

Best regards, loviji

loviji
  • 12,620
  • 17
  • 63
  • 94

3 Answers3

13

The other answers are correct. Here is some code you could use:

ProcessStartInfo startInfo = new ProcessStartInfo(cmd, args);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
Process someProcess = Process.Start(startInfo);
string errors = someProcess.StandardError.ReadToEnd();

Note that if the file is not found you won't get an error on standard error. You will get an exception instead.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Note that the call to ReadToEnd might never terminate. To avoid deadlock use the asynchronous read methods instead. See my answer here: http://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why/7608823#7608823 – Mark Byers Nov 09 '11 at 14:37
  • I follow your ans. and got this error in errors (string var.) 'casperjs' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n Plz give any solution if exist – Unknown_Coder Oct 10 '17 at 06:48
2

You're probably looking for the StandardError property.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Process.StandardError Property:

Gets a stream used to read the error output of the application.

This should do what you want.

Note

To use StandardError, you must set ProcessStartInfo.UseShellExecute to false, and you must set ProcessStartInfo.RedirectStandardError to true. Otherwise, reading from the StandardError stream throws an exception.

ChrisF
  • 134,786
  • 31
  • 255
  • 325