首先,进程涉及的流有三种,输入流StandardInput、输出流StandardOutput、错误流StandardError,三者不可混淆,特别是输出流和错误流更是要注意不可混为一谈!比如在makefile编译过程中执行make命令,如果没有将错误流重定向输出,那么程序是不会显示原本应该打印的错误信息的。
一、同步方式
这里有一个简单编写的小例子,是一个win窗口程序。为点击按钮,在后台启动控制台,按照程序设定输入命令,并将返回的输出打印在文本框中,下面是核心代码——按钮点击的响应函数。这里存在着有诸多问题:
private void button1_Click(object sender, EventArgs e) { ProcessStartInfo start = new ProcessStartInfo("cmd.exe"); start.CreateNoWindow = true; //不显示dos命令行窗口 start.RedirectStandardOutput = true; start.RedirectStandardInput = true; start.RedirectStandardError = true; start.UseShellExecute = false; //是否指定操作系统外壳进程启动程序,这里需为false Process p &#