为了账号安全,请及时绑定邮箱和手机立即绑定

如何:在C#中执行命令行,获取STD输出结果

如何:在C#中执行命令行,获取STD输出结果

C#
倚天杖 2019-06-06 10:27:10
如何:在C#中执行命令行,获取STD输出结果如何从C#执行命令行程序并返回STD的输出结果?具体来说,我希望对以编程方式选择的两个文件执行diff,并将结果写入文本框。
查看完整描述

3 回答

?
Helenr

TA贡献1780条经验 获得超4个赞

// 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 = "YOURBATCHFILE.bat";
 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();

代码来自MSDN.


查看完整回答
反对 回复 2019-06-06
?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

还有一个参数是有用的,我用它来消除流程窗口。

pProcess.StartInfo.CreateNoWindow = true;

这有助于完全对用户隐藏黑色控制台窗口,如果这是您所希望的。


查看完整回答
反对 回复 2019-06-06
  • 3 回答
  • 0 关注
  • 830 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信