111,092
社区成员




Process p; //进程
StreamWriter input; //输入数据流
public Form1()
{
InitializeComponent();
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;//自定义shell
p.StartInfo.CreateNoWindow = true;//避免显示原始窗口
p.StartInfo.RedirectStandardInput = true;//重定向标准输入(原来是CON)
p.StartInfo.RedirectStandardOutput = true;//重定向标准输出
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
//数据接收事件(标准输出重定向至此)
p.Start();//GO
input = p.StandardInput;//重定向输入
p.BeginOutputReadLine();//开始监控输出(异步读取)
}
private void button1_Click(object sender, EventArgs e)
{
input.WriteLine(textBox1.Text);//直接写入流即可
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
update(e.Data + Environment.NewLine);
}
delegate void updateDelegate(string msg);
void update(string msg)
{
if (this.InvokeRequired)
Invoke(new updateDelegate(update), new object[] { msg });
else
{
textBox2.Text += msg;
}
}