如何通过外壳执行命令,并使用C#以字符串形式返回完整的输出?
相当于PHP的shell_exec()。
谢谢,进阶。
发布于 2011-06-22 05:07:23
MSDN documentation on Process.StandardOutput文档显示了一个示例
// 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-22 04:59:53
使用Process类
发布于 2011-06-22 05:02:08
看看Process类Standard Output和Standard Error,以及OutputDataReceived和ErrorDataReceived接收的事件。
有一个CodeProject article here,你可能会发现它很有帮助。
https://stackoverflow.com/questions/6431886
复制相似问题