日期:2014-05-18 浏览次数:20965 次
using System;
using System.Windows.Forms;
namespace WindowsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
delegate void dReadLine(string strLine);
private void excuteCommand(string strFile, string args, dReadLine onReadLine)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo();
p.StartInfo.FileName = strFile;
p.StartInfo.Arguments = args;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
System.IO.StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
onReadLine(line);
line = reader.ReadLine();
}
p.WaitForExit();
}
private void button1_Click(object sender, EventArgs e)
{
excuteCommand("ipconfig", "", new dReadLine(PrintMessage));
}
private void PrintMessage(string strLine)
{
this.textBox1.Text += strLine + " ";
Application.DoEvents();
}
}
}
------解决方案--------------------
完成这个工作的时间比较长,主线程就一直在进行处理,所以没有反应,程序像死了
你可以自己创建个线程进行处理
------解决方案--------------------
加上这句Application.DoEvents();,像窗体代码继续运行,调用DOS让他自己去执行去
------解决方案--------------------
c#中运行命令行截取输出流的例子
说明:经常有朋友问如何在c#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在java中实现过,由于在c#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在winform中ping一个网站,并且将ping的结果显示在一个