问题描述
|
如何通过Shell执行命令并使用C#将完整的输出作为字符串返回?
等效于PHP的shell_exec()。
谢谢,谢谢。
解决方法
有关Process.StandardOutput文档的MSDN文档显示了一个示例
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = \"Write500Lines.exe\";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
您可能还希望提取标准错误流。
, 使用流程类
, 查看Process类Standard Output和Standard Error,以及OutputDataReceived和
ErrorDataReceived收到的事件。
这里有一个CodeProject文章,您可能会发现有帮助。