如何:在C#中执行命令行,获取STD OUT结果 [英] How To: Execute command line in C#, get STD OUT results

查看:105
本文介绍了如何:在C#中执行命令行,获取STD OUT结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#执行命令行程序并返回STD OUT结果。具体来说,我想对编程选择的两个文件执行DIFF,并将结果写入文本框。是的,我可以为自己找到,但是肯定有人做了类似的东西,我懒惰...

How do I execute a command-line program from C# and get back the STD OUT results. Specifically, I want to execute DIFF on two files that are programmatically selected and write the results to a text box. Yes, I could figure this out for myself, but surely someone else has done something like it and I'm lazy...

推荐答案

// 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

这篇关于如何:在C#中执行命令行,获取STD OUT结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆