1. 程式人生 > 其它 >C# 動態執行Dos命令並顯示輸出命令執行結果

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 }

備註:

關於如何在命令列執行過程中【如:Ping 192.168.1.100 -t】,鍵入快捷鍵【如:Ctrl+C】等操作,目前還沒有實現。目前採用的就強制關閉程序方法

原始碼下載

出處:https://www.cnblogs.com/hsiang/p/6596276.html

您的資助是我最大的動力!
金額隨意,歡迎來賞!
款後有任何問題請給我留言。

如果,您認為閱讀這篇部落格讓您有些收穫,不妨點選一下右下角的推薦按鈕。
如果,您希望更容易地發現我的新部落格,不妨點選一下綠色通道的關注我。(●'◡'●)

如果對你有所幫助,贊助一杯咖啡!打 付款後有任何問題請給我留言!!!

因為,我的寫作熱情也離不開您的肯定與支援,感謝您的閱讀,我是【Jack_孟】!