本文以一個簡單的小例子講解如何將命令行信息實時的輸出到文本框中。僅供學習分享使用,如有不足之處,還請指正。
概述
在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 }
備注:
關於如何在命令行執行過程中【如:Ping 192.168.1.100 -t】,鍵入快捷鍵【如:Ctrl+C】等操作,目前還沒有實現。目前采用的就強制關閉進程方法
出處:https://www.cnblogs.com/hsiang/p/6596276.html