Possible Duplicate:
Command prompt output being read as empty string
I have a command line program which must be executed at least once before my code can do it’s groovy thang. So I use Process to run it. This appears to work.
My problem lies in the fact that I’d like to be able to see what the program says as it completes. In some situations it may return an error, which would require user intervention before proceeding. On paper, this seems trivial. Sadly, my code (below) does not seem to work:
Process p = new Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = String.Format("/C adb forward tcp:{0} tcp:5574",port.ToString());
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
p.Start();
string adbResponse = p.StandardOutput.ReadToEnd();
p.WaitForExit();
if (adbResponse.Contains("error"))
{
// Device not connected - complain loudly
}
When I try doing this on a CMD window of my own creation, I am able to reliably induce a response containing the word error. (Specifically by unplugging something.) Under the same conditions however, the adbResponse string remains empty. What am I missing?
Console-attached processes have two different output streams. You are trapping
StandardOutputbut you probably want to be catchingStandardError. See this question for a complete explanation (and code to safely capture both without deadlocking):Command prompt output being read as empty string