3

The code is as follows:

ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c" + command);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = arguments;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;

Process process = Process.start(startInfo);
StreamReader srOutput = process.StandardOutput;
string output = srOutput.ReadToEnd();

The command is rmdir /s /q 123

I expect to get "The system cannot find the file specified" inside the variable output because "123" is a file path that doesn't exist. But output is an empty string. Why and how should I go about getting the output?

1
  • still got doubts, see comment below... Commented Jun 2, 2011 at 7:17

1 Answer 1

5

The message you're expecting to see will be onStandardError, not StandardOutput.

Sign up to request clarification or add additional context in comments.

4 Comments

Why is it sometimes the expected error msg is in the output instead? For example the MOVE command, if I didn't put the open and close inverted commas for destination path. The "The syntax if the command is incorrect" msg is in the 'StandardOutput' instead of the 'StandardError' and the 'StandardError' is empty.
You are using the command line processor. Having a user mis-type a command is perhaps far too common to call it an error. Just don't use cmd.exe to do this, use the classes in the System.IO namespace instead.
@yeeen - Just inconsistency on the part of Microsoft in writing the code that executes the commands.
@Hans What do u mean by "use the classes in the System.IO namespace" instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.