這個問題來自論壇提問,答案如下.這只是一個簡單的ipconfig命令.如果是復雜的,比如oracle的exp之類的命令,能在調用的時候顯示出來,還是相當酷的.
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 + " ";
}
}
}