C#相当于shell_exec

问题描述

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...