當控制台命令使用process.Start(); 後可以直接顯示輸出內容,當然它是異步顯示的不用等程序結束。代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace testApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = "ping";
process.StartInfo.Arguments = "127.0.0.1 -t";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.BeginOutputReadLine();
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
}
}
private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
this.AppendText(e.Data + Environment.NewLine);
}
public delegate void AppendTextCallback(string text);
&nb