如何通过 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 文章,您可能会发现它很有帮助。