>在C#应用程序中捕获命令行输出
> 在C#开发中,您通常需要运行命令行应用程序并检索其输出。这对于诸如文件比较之类的任务至关重要(使用之类的工具)。 diff
类提供了强大的解决方案。这是逐步指南:Process
启动外部进程:
<code class="language-csharp">Process process = new Process();</code>
>
<code class="language-csharp">process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true;</code>
<code class="language-csharp">process.StartInfo.FileName = "YOURBATCHFILE.bat"; // Or any executable path</code>
<code class="language-csharp">process.Start();</code>
<code class="language-csharp">string output = process.StandardOutput.ReadToEnd();</code>等待过程完成:
字符串变量现在将保留命令的标准输出。
<code class="language-csharp">process.WaitForExit();</code>基于MSDN文档,这种方法演示了
以上是如何在C#中检索命令行输出?的详细内容。更多信息请关注PHP中文网其他相关文章!