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

简介:
说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。
运行结果图
窗体设计器产生的代码:
namespace RunCMD 

        partial  class CMDForm 
        { 
                 /// <summary> 
                 /// 必需的设计器变量。 
                 /// </summary> 
                 private System.ComponentModel.IContainer components =  null
 
                 /// <summary> 
                 /// 清理所有正在使用的资源。 
                 /// </summary> 
                 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false</param> 
                 protected  override  void Dispose( bool disposing) 
                { 
                         if (disposing && (components !=  null)) 
                        { 
                                components.Dispose(); 
                        } 
                         base.Dispose(disposing); 
                } 
 
Windows 窗体设计器生成的代码 #region Windows 窗体设计器生成的代码 
 
                 /// <summary> 
                 /// 设计器支持所需的方法 - 不要 
                 /// 使用代码编辑器修改此方法的内容。 
                 /// </summary> 
                 private  void InitializeComponent() 
                { 
                         this.label1 =  new System.Windows.Forms.Label(); 
                         this.txtCommand =  new System.Windows.Forms.TextBox(); 
                         this.btnExecute =  new System.Windows.Forms.Button(); 
                         this.tbResult =  new System.Windows.Forms.TextBox(); 
                         this.SuspendLayout(); 
                         //    
                         // label1 
                         //    
                         this.label1.AutoSize =  true
                         this.label1.Location =  new System.Drawing.Point(6, 11); 
                         this.label1.Name =  "label1"
                         this.label1.Size =  new System.Drawing.Size(29, 12); 
                         this.label1.TabIndex = 0; 
                         this.label1.Text =  "ping"
                         //    
                         // txtCommand 
                         //    
                         this.txtCommand.Location =  new System.Drawing.Point(41, 8); 
                         this.txtCommand.Name =  "txtCommand"
                         this.txtCommand.Size =  new System.Drawing.Size(269, 21); 
                         this.txtCommand.TabIndex = 1; 
                         //    
                         // btnExecute 
                         //    
                         this.btnExecute.Location =  new System.Drawing.Point(316, 6); 
                         this.btnExecute.Name =  "btnExecute"
                         this.btnExecute.Size =  new System.Drawing.Size(75, 23); 
                         this.btnExecute.TabIndex = 2; 
                         this.btnExecute.Text =  "执行"
                         this.btnExecute.UseVisualStyleBackColor =  true
                         this.btnExecute.Click +=  new System.EventHandler( this.btnExecute_Click); 
                         //    
                         // tbResult 
                         //    
                         this.tbResult.Location =  new System.Drawing.Point(8, 47); 
                         this.tbResult.Multiline =  true
                         this.tbResult.Name =  "tbResult"
                         this.tbResult.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
                         this.tbResult.Size =  new System.Drawing.Size(383, 292); 
                         this.tbResult.TabIndex = 3; 
                         //    
                         // CMDForm 
                         //    
                         this.AutoScaleDimensions =  new System.Drawing.SizeF(6F, 12F); 
                         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
                         this.ClientSize =  new System.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 
 
                 private System.Windows.Forms.Label label1; 
                 private System.Windows.Forms.TextBox txtCommand; 
                 private System.Windows.Forms.Button btnExecute; 
                 private System.Windows.Forms.TextBox tbResult; 
        } 
}
 
关键部分代码:
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 
using System.IO; 
 
namespace RunCMD 

         /** 
         * 作者:周公 
         * blog:http://blog.csdn.net/zhoufoxcn 
         * 日期:2007-07-07 
         *    
         * */
 
         public partial  class CMDForm : Form 
        { 
                 public CMDForm() 
                { 
                        InitializeComponent(); 
                } 
 
                 private  void btnExecute_Click( object sender, EventArgs e) 
                { 
                        tbResult.Text = ""; 
                        ProcessStartInfo start =  new ProcessStartInfo( "Ping.exe"); //设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 
                         //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe 
                        start.Arguments = txtCommand.Text; //设置命令参数 
                        start.CreateNoWindow =  true; //不显示dos命令行窗口 
                        start.RedirectStandardOutput =  true; // 
                        start.RedirectStandardInput =  true; // 
                        start.UseShellExecute =  false; //是否指定操作系统外壳进程启动程序 
                        Process p=Process.Start(start); 
                        StreamReader reader = p.StandardOutput; //截取输出流 
                         string line = reader.ReadLine(); //每次读取一行 
                         while (!reader.EndOfStream) 
                        { 
                                tbResult.AppendText(line+ " "); 
                                line = reader.ReadLine(); 
                        } 
                        p.WaitForExit(); //等待程序执行完退出进程 
                        p.Close(); //关闭进程 
                        reader.Close(); //关闭流 
                } 
        } 

 
本demo全部代码:http://dl2.csdn.net/down4/20070707/07162550531.rar

 















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

相关文章
C#有关字符串的分割,替换,截取
C#有关字符串的分割,替换,截取
|
缓存 C#
C# 操作路径(Path)类方法的使用与解析运行实例
C# 操作路径(Path)类方法的使用与解析运行实例
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
|
C# iOS开发 MacOS
MacOS操作系统当中运行VSCode并配置运行调试C#项目
在开发的过程当中,经常会遇到各种开发环境,在MacOS上如何运行VSCode,配置并且调试C#项目,本文进行讲解
2497 0
MacOS操作系统当中运行VSCode并配置运行调试C#项目
|
4月前
|
C# 开发工具 C++
code runner 运行C#项目
本文介绍了如何修改Code Runner设置使 Visual Studio Code (VS Code) 能直接运行完整的 C# 项目。传统方式依赖 cscript 工具,仅支持 .csx 文件,功能受限且已停止维护。新配置使用 `dotnet run` 命令,结合一系列炫酷的cmd指令,将指令定位到具体的csproj文件上进行运行。
215 38
|
5月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
114 12
|
7月前
|
C#
C#实现的html内容截取.
C#实现的html内容截取.
45 0
|
9月前
|
Linux C#
【Azure App Service】C#下制作的网站,所有网页本地测试运行无误,发布至Azure之后,包含CHART(图表)的网页打开报错,错误消息为 Runtime Error: Server Error in '/' Application
【Azure App Service】C#下制作的网站,所有网页本地测试运行无误,发布至Azure之后,包含CHART(图表)的网页打开报错,错误消息为 Runtime Error: Server Error in '/' Application
|
关系型数据库 API C#
C#调用执行命令行窗口cmd,及需要交互执行的处理
C#执行外部程序用到的是Process进程类,打开一个进程,可以指定进程的启动信息StartInfo(启动的程序名、输入输出是否重定向、是否显示UI界面、一些必要参数等)...
3510 0
C#调用执行命令行窗口cmd,及需要交互执行的处理