C# 动态输出Dos命令执行结果

本文以一个简单的小例子讲解如何将命令行信息实时的输出到文本框中。仅供学习分享使用,如有不足之处,还请指正。

概述

在C#程序开发过程中,有时需要运行其它的程序并获得输出的结果来进行进一步的处理。一般第三方的程序,主要通过进程来调用,如果能够获取第三方程序执行过程中的信息,就显得方便而有用。

涉及知识点:

  •  进程相关类: Process,ProcessStartInfo,主要设置进程的重定向输出,以及接受数据的事件。
  •  文本框操作:多行显示,滚动条总在最下面

效果图

如下【如果命令执行完毕,会自动结束,如果中断进程,可以手动点击结束进程】:

核心代码

主要代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Diagnostics;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Runtime.InteropServices;
  9 using System.Text;
 10 using System.Threading;
 11 using System.Threading.Tasks;
 12 using System.Windows.Forms;
 13 
 14 namespace DemoBat
 15 {
 16     public partial class MainForm : Form
 17     {
 18         private BatStatus curBatSataus = BatStatus.NONE;
 19 
 20         private Process curProcess = new Process();
 21 
 22         public MainForm()
 23         {
 24             InitializeComponent();
 25         }
 26 
 27         private void MainForm_Load(object sender, EventArgs e)
 28         {
 29             InitInfo();
 30         }
 31 
 32         private void InitInfo()
 33         {
 34             curProcess.OutputDataReceived -= new DataReceivedEventHandler(ProcessOutDataReceived);
 35             ProcessStartInfo p = new ProcessStartInfo();
 36             p.FileName = "cmd.exe";
 37             //p.Arguments = " -t 192.168.1.103";
 38             p.UseShellExecute = false;
 39             p.WindowStyle = ProcessWindowStyle.Hidden;
 40             p.CreateNoWindow = true;
 41             p.RedirectStandardError = true;
 42             p.RedirectStandardInput = true;
 43             p.RedirectStandardOutput = true;
 44             curProcess.StartInfo = p;
 45             curProcess.Start();
 46 
 47             curProcess.BeginOutputReadLine();
 48             curProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutDataReceived);
 49         }
 50 
 51         /// <summary>
 52         /// 开始命令行
 53         /// </summary>
 54         /// <param name="sender"></param>
 55         /// <param name="e"></param>
 56         private void btnStart_Click(object sender, EventArgs e)
 57         {
 58             if (string.IsNullOrEmpty(this.txtCommand.Text.Trim()))
 59             {
 60                 MessageBox.Show("请输入命令");
 61             }
 62             if (curBatSataus != BatStatus.ON)
 63             {
 64                 curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim());
 65                 curBatSataus = BatStatus.ON;
 66             }
 67             else {
 68                 MessageBox.Show("当前进程正在运行,请先关闭");
 69             }
 70 
 71         }
 72 
 73         /// <summary>
 74         /// 结束命令行
 75         /// </summary>
 76         /// <param name="sender"></param>
 77         /// <param name="e"></param>
 78         private void btnStop_Click(object sender, EventArgs e)
 79         {
 80             if (curBatSataus == BatStatus.ON)
 81             {
 82                 curProcess.CancelOutputRead();//取消异步操作
 83                 curProcess.Kill();
 84                 curBatSataus = BatStatus.OFF;
 85                 //如果需要手动关闭,则关闭后再进行初始化
 86                 InitInfo();
 87             }
 88         }
 89 
 90         /// <summary>
 91         /// 进程接受事件
 92         /// </summary>
 93         /// <param name="sender"></param>
 94         /// <param name="e"></param>
 95         public void ProcessOutDataReceived(object sender, DataReceivedEventArgs e)
 96         {
 97             if (this.txtOutPutInfo.InvokeRequired)
 98             {
 99                 this.txtOutPutInfo.Invoke(new Action(() =>
100                 {
101                     this.txtOutPutInfo.AppendText(e.Data + "\r\n");
102                 }));
103             }
104             else {
105                 this.txtOutPutInfo.AppendText(e.Data + "\r\n");
106             }
107         }
108 
109         private void timer1_Tick(object sender, EventArgs e)
110         {
111             if ((string.IsNullOrEmpty(this.curProcess.StartInfo.FileName) || this.curProcess.StandardInput.BaseStream.CanWrite) && curBatSataus != BatStatus.OFF)
112             {
113                 curBatSataus = BatStatus.OFF;
114 
115             }
116         }
117 
118     }
119 
120     /// <summary>
121     /// 命令状态
122     /// </summary>
123     public enum BatStatus {
124         NONE = 0,
125         ON = 1,
126         OFF = 2
127     }
128 }
View Code

备注:

关于如何在命令行执行过程中【如:Ping 192.168.1.100 -t】,键入快捷键【如:Ctrl+C】等操作,目前还没有实现。目前采用的就强制关闭进程方法

源码下载

posted @ 2017-03-21 19:35  老码识途呀  阅读(4182)  评论(0编辑  收藏  举报