C#中运行命令行截取输出流的例子

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

 















本文转自周金桥51CTO博客,原文链接: http://blog.51cto.com/zhoufoxcn/166055 ,如需转载请自行联系原作者

相关文章
|
4月前
|
缓存 C#
C# 操作路径(Path)类方法的使用与解析运行实例
C# 操作路径(Path)类方法的使用与解析运行实例
|
6月前
|
C#
C#程序Debug文件夹可以运行,无法调试
C#程序Debug文件夹可以运行,无法调试
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
|
4月前
|
编译器 应用服务中间件 数据库连接
解决C#运行程序修改数据后数据表不做更新的问题
解决C#运行程序修改数据后数据表不做更新的问题
41 0
|
10月前
|
XML 存储 测试技术
在C#下运行Python:IronPython和Pythonnet
在C#下运行Python可能有不同的原因。其中一些原因包括: 1. 使用C#应用程序中不可用的特定Python功能或库。 2. 结合Python的简单性和表现力以及C#的性能和稳健性,完成不同任务。 3. 与基于Python的系统或服务进行集成。
141 0
|
12月前
|
C#
C# 判断当前控制台程序是否重复运行
C# 判断当前控制台程序是否重复运行
123 0
|
C# C++ Windows
C#工程中输出类型转换以及程序运行后控制台窗口不退出设置
C#工程中输出类型转换以及程序运行后控制台窗口不退出设置
302 0
C#工程中输出类型转换以及程序运行后控制台窗口不退出设置
|
C# 数据安全/隐私保护 Windows
C# Windows服务以指定用户运行
参考一下 https://bbs.csdn.net/topics/330151879 服务程序以Local System安装运行没问题,但用这个账户运行的服务无法访问局域网共享资源,比较麻烦,所以想指定用某个账户来启动服务。
1912 0