这个问题来自论坛提问,答案如下.这只是一个简单的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,dReadLineonReadLine)
...
{
System.Diagnostics.Processp
=
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.StreamReaderreader
=
p.StandardOutput;
//
截取输出流
string
line
=
reader.ReadLine();
//
每次读取一行
while
(
!
reader.EndOfStream)
...
{
onReadLine(line);
line
=
reader.ReadLine();
}
p.WaitForExit();
}

private
void
button1_Click(
object
sender,EventArgse)
...
{
excuteCommand(
"
ipconfig
"
,
""
,
new
dReadLine(PrintMessage));
}
private
void
PrintMessage(
string
strLine)
...
{
this
.textBox1.Text
+=
strLine
+
"
"
;
}
}
}
本文介绍了一个简单的C#程序,该程序能够调用命令行工具并实时显示输出结果,例如使用ipconfig命令获取网络配置信息。通过创建委托和使用Process类,可以实现对外部命令的调用,并通过事件监听的方式捕捉命令执行过程中的输出。
1100

被折叠的 条评论
为什么被折叠?



