cmd窗口获取数据,不间断ping
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 测试另外一套代码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.IO.StreamWriter sw; // 定义输出流 sw 作为Shell的标准输入,即命令
System.IO.StreamReader sr; // 定义输出流 sr 作为Shell的标准输出,即正常结果
System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"));
delegate void chandle(string s);
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread tt = new Thread(new ThreadStart(t));
tt.Start();
}
private void t()
{
//p.StartInfo.WorkingDirectory = @"E:\TQ\";
p.StartInfo.FileName = @"E:\TQ\tq.exe";
//p.StartInfo.Arguments = @"I:\kn\m12\m12..ac3 I:\kn\m12\au2.wav";
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);//这里多了这句
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
//p.StartInfo.CreateNoWindow = true;
p.Start();
//p.StandardInput.WriteLine("ping www.china.com -t");
//p.StartInfo.Arguments = " www.china.com -t";
//p.BeginErrorReadLine(); //这里多了这句
p.BeginOutputReadLine();
p.WaitForExit();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{ // 做点工作
Invoke(new chandle(ct), e.Data);
}
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{ // 做点工作
Invoke(new chandle(ct), e.Data);
}
}
private void ct(string s)
{
textBox1.AppendText(s.Trim() + "\n");
textBox1.SelectionStart = textBox1.Text.Length - 1;
Application.DoEvents();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//p.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;
sw.AutoFlush = true;
if (coboCommand.Text != "")
{
sw.WriteLine(coboCommand.Text);
}
else
{
sw.WriteLine("echo 未输入命令");
}
sw.Close();
textBox1.Text = "输出结果为:" + sr.ReadToEnd();
textBox1.Text += "\n错误信息:\n" + err.ReadToEnd();
Cursor = System.Windows.Forms.Cursors.Default;
}
private void coboCommand_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace 测试另外一套代码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.IO.StreamWriter sw; // 定义输出流 sw 作为Shell的标准输入,即命令
System.IO.StreamReader sr; // 定义输出流 sr 作为Shell的标准输出,即正常结果
System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"));
delegate void chandle(string s);
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread tt = new Thread(new ThreadStart(t));
tt.Start();
}
private void t()
{
//p.StartInfo.WorkingDirectory = @"E:\TQ\";
p.StartInfo.FileName = @"E:\TQ\tq.exe";
//p.StartInfo.Arguments = @"I:\kn\m12\m12..ac3 I:\kn\m12\au2.wav";
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);//这里多了这句
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
//p.StartInfo.CreateNoWindow = true;
p.Start();
//p.StandardInput.WriteLine("ping www.china.com -t");
//p.StartInfo.Arguments = " www.china.com -t";
//p.BeginErrorReadLine(); //这里多了这句
p.BeginOutputReadLine();
p.WaitForExit();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{ // 做点工作
Invoke(new chandle(ct), e.Data);
}
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{ // 做点工作
Invoke(new chandle(ct), e.Data);
}
}
private void ct(string s)
{
textBox1.AppendText(s.Trim() + "\n");
textBox1.SelectionStart = textBox1.Text.Length - 1;
Application.DoEvents();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//p.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;
sw.AutoFlush = true;
if (coboCommand.Text != "")
{
sw.WriteLine(coboCommand.Text);
}
else
{
sw.WriteLine("echo 未输入命令");
}
sw.Close();
textBox1.Text = "输出结果为:" + sr.ReadToEnd();
textBox1.Text += "\n错误信息:\n" + err.ReadToEnd();
Cursor = System.Windows.Forms.Cursors.Default;
}
private void coboCommand_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
posted on 2011-04-12 23:22 welcomesay 阅读(998) 评论(0) 编辑 收藏 举报