【问题标题】:C# equivalent to shell_execC# 等价于 shell_exec
【发布时间】:2011-09-19 21:17:06
【问题描述】:

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

相当于PHP的shell_exec()

谢谢,高级。

【问题讨论】:

    标签: c# .net cmd shell-exec


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      有一个CodeProject article here,您可能会觉得有帮助。

      【讨论】:

        【解决方案3】:

        使用Process

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多