C#中运行命令行截取输出流的例子
说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。
运行结果图
窗体设计器产生的代码:
namespaceRunCMD
{
partialclassCMDForm
{
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;
///<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">如果应释放托管资源,为true;否则为false。</param>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///<summary>
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.label1=newSystem.Windows.Forms.Label();
this.txtCommand=newSystem.Windows.Forms.TextBox();
this.btnExecute=newSystem.Windows.Forms.Button();
this.tbResult=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//label1
//
this.label1.AutoSize=true;
this.label1.Location=newSystem.Drawing.Point(6,11);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(29,12);
this.label1.TabIndex=0;
this.label1.Text="ping";
//
//txtCommand
//
this.txtCommand.Location=newSystem.Drawing.Point(41,8);
this.txtCommand.Name="txtCommand";
this.txtCommand.Size=newSystem.Drawing.Size(269,21);
this.txtCommand.TabIndex=1;
//
//btnExecute
//
this.btnExecute.Location=newSystem.Drawing.Point(316,6);
this.btnExecute.Name="btnExecute";
this.btnExecute.Size=newSystem.Drawing.Size(75,23);
this.btnExecute.TabIndex=2;
this.btnExecute.Text="执行";
this.btnExecute.UseVisualStyleBackColor=true;
this.btnExecute.Click+=newSystem.EventHandler(this.btnExecute_Click);
//
//tbResult
//
this.tbResult.Location=newSystem.Drawing.Point(8,47);
this.tbResult.Multiline=true;
this.tbResult.Name="tbResult";
this.tbResult.ScrollBars=System.Windows.Forms.ScrollBars.Both;
this.tbResult.Size=newSystem.Drawing.Size(383,292);
this.tbResult.TabIndex=3;
//
//CMDForm
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(403,364);
this.Controls.Add(this.tbResult);
this.Controls.Add(this.btnExecute);
this.Controls.Add(this.txtCommand);
this.Controls.Add(this.label1);
this.Name="CMDForm";
this.Text="运行Command的例子";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtxtCommand;
privateSystem.Windows.Forms.ButtonbtnExecute;
privateSystem.Windows.Forms.TextBoxtbResult;
}
}
{
partialclassCMDForm
{
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;
///<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">如果应释放托管资源,为true;否则为false。</param>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///<summary>
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.label1=newSystem.Windows.Forms.Label();
this.txtCommand=newSystem.Windows.Forms.TextBox();
this.btnExecute=newSystem.Windows.Forms.Button();
this.tbResult=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//label1
//
this.label1.AutoSize=true;
this.label1.Location=newSystem.Drawing.Point(6,11);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(29,12);
this.label1.TabIndex=0;
this.label1.Text="ping";
//
//txtCommand
//
this.txtCommand.Location=newSystem.Drawing.Point(41,8);
this.txtCommand.Name="txtCommand";
this.txtCommand.Size=newSystem.Drawing.Size(269,21);
this.txtCommand.TabIndex=1;
//
//btnExecute
//
this.btnExecute.Location=newSystem.Drawing.Point(316,6);
this.btnExecute.Name="btnExecute";
this.btnExecute.Size=newSystem.Drawing.Size(75,23);
this.btnExecute.TabIndex=2;
this.btnExecute.Text="执行";
this.btnExecute.UseVisualStyleBackColor=true;
this.btnExecute.Click+=newSystem.EventHandler(this.btnExecute_Click);
//
//tbResult
//
this.tbResult.Location=newSystem.Drawing.Point(8,47);
this.tbResult.Multiline=true;
this.tbResult.Name="tbResult";
this.tbResult.ScrollBars=System.Windows.Forms.ScrollBars.Both;
this.tbResult.Size=newSystem.Drawing.Size(383,292);
this.tbResult.TabIndex=3;
//
//CMDForm
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(403,364);
this.Controls.Add(this.tbResult);
this.Controls.Add(this.btnExecute);
this.Controls.Add(this.txtCommand);
this.Controls.Add(this.label1);
this.Name="CMDForm";
this.Text="运行Command的例子";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtxtCommand;
privateSystem.Windows.Forms.ButtonbtnExecute;
privateSystem.Windows.Forms.TextBoxtbResult;
}
}
关键部分代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Diagnostics;
usingSystem.IO;
namespaceRunCMD
{
/**
*作者:周公
*blog:http://blog.****.net/zhoufoxcn
*日期:2007-07-07
*
**/
publicpartialclassCMDForm:Form
{
publicCMDForm()
{
InitializeComponent();
}
privatevoidbtnExecute_Click(objectsender,EventArgse)
{
tbResult.Text="";
ProcessStartInfostart=newProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments=txtCommand.Text;//设置命令参数
start.CreateNoWindow=true;//不显示dos命令行窗口
start.RedirectStandardOutput=true;//
start.RedirectStandardInput=true;//
start.UseShellExecute=false;//是否指定操作系统外壳进程启动程序
Processp=Process.Start(start);
StreamReaderreader=p.StandardOutput;//截取输出流
stringline=reader.ReadLine();//每次读取一行
while(!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line=reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
}
}
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Diagnostics;
usingSystem.IO;
namespaceRunCMD
{
/**
*作者:周公
*blog:http://blog.****.net/zhoufoxcn
*日期:2007-07-07
*
**/
publicpartialclassCMDForm:Form
{
publicCMDForm()
{
InitializeComponent();
}
privatevoidbtnExecute_Click(objectsender,EventArgse)
{
tbResult.Text="";
ProcessStartInfostart=newProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments=txtCommand.Text;//设置命令参数
start.CreateNoWindow=true;//不显示dos命令行窗口
start.RedirectStandardOutput=true;//
start.RedirectStandardInput=true;//
start.UseShellExecute=false;//是否指定操作系统外壳进程启动程序
Processp=Process.Start(start);
StreamReaderreader=p.StandardOutput;//截取输出流
stringline=reader.ReadLine();//每次读取一行
while(!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line=reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
}
}
本demo全部代码:http://dl2.****.net/down4/20070707/07162550531.rar