loviji
loviji

Reputation: 13080

Process.Start() get errors from command prompt window

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

Upvotes: 13

Views: 18098

Answers (3)

Mark Byers
Mark Byers

Reputation: 838156

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.

Upvotes: 13

ChrisF
ChrisF

Reputation: 137138

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.

Upvotes: 2

SLaks
SLaks

Reputation: 887415

You're probably looking for the StandardError property.

Upvotes: 2

Related Questions