6

如何通过 shell 执行命令并使用 C# 将完整的输出作为字符串返回?

相当于 PHP 的shell_exec()

谢谢,高级。

4

3 回答 3

5

使用进程

于 2011-06-21T20:59:52.540 回答
5

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();

您可能还想提取标准错误流。

于 2011-06-21T21:07:22.923 回答
1

查看 Process 类Standard OutputStandard Error,以及OutputDataReceivedErrorDataReceived收到的事件。

这里有一篇CodeProject 文章,您可能会发现它很有帮助。

于 2011-06-21T21:02:07.850 回答