在安装了mono的raspberry pi(linux)上运行在visual studio 2013中编译的c控制台应用程序,下面的代码…
System.Diagnostics.Process syncProc = new System.Diagnostics.Process();
syncProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
syncProc.StartInfo.UseShellExecute = false;
syncProc.StartInfo.FileName = "bash";
syncProc.StartInfo.Arguments = "-c sync --help";
syncProc.StartInfo.RedirectStandardError = true;
syncProc.StartInfo.RedirectStandardOutput = true;
syncProc.Start();
String stdOutput = syncProc.StandardOutput.ReadToEnd();
String errorOutput = syncProc.StandardError.ReadToEnd();
syncProc.WaitForExit();
if (syncProc.ExitCode == 0)
{
Console.WriteLine("SOUTPUT: " + stdOutput);
}
else
{
Console.WriteLine("EOUTPUT: " + errorOutput);
throw new Exception("Failed to sync with exit code: " + syncProc.ExitCode);
}
…输出“soutput:”
为什么stdoutput总是空的?
最佳答案
您可以尝试异步重定向整个输出,如下所示:
syncProc.ErrorDataReceived += (s, e) => Console.WriteLine("EOUTPUT:{0}", e.Data);
syncProc.OutputDataReceived += (s, e) => Console.WriteLine("SOUTPUT:{0}", e.Data);
syncProc.Start();
syncProc.BeginErrorReadLine();
syncProc.BeginOutputReadLine();