C# 動態(tài)輸出Dos命令執(zhí)行結(jié)果的實例(附源碼)
本文以一個簡單的小例子講解如何將命令行信息實時的輸出到文本框中。僅供學習分享使用,如有不足之處,還請指正。
概述
在C#程序開發(fā)過程中,有時需要運行其它的程序并獲得輸出的結(jié)果來進行進一步的處理。一般第三方的程序,主要通過進程來調(diào)用,如果能夠獲取第三方程序執(zhí)行過程中的信息,就顯得方便而有用。
涉及知識點:
- 進程相關(guān)類: Process,ProcessStartInfo,主要設置進程的重定向輸出,以及接受數(shù)據(jù)的事件。
- 文本框操作:多行顯示,滾動條總在最下面
效果圖
如下【如果命令執(zhí)行完畢,會自動結(jié)束,如果中斷進程,可以手動點擊結(jié)束進程】:
核心代碼
主要代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace DemoBat { public partial class MainForm : Form { private BatStatus curBatSataus = BatStatus.NONE; private Process curProcess = new Process(); public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { InitInfo(); } private void InitInfo() { curProcess.OutputDataReceived -= new DataReceivedEventHandler(ProcessOutDataReceived); ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "cmd.exe"; //p.Arguments = " -t 192.168.1.103"; p.UseShellExecute = false; p.WindowStyle = ProcessWindowStyle.Hidden; p.CreateNoWindow = true; p.RedirectStandardError = true; p.RedirectStandardInput = true; p.RedirectStandardOutput = true; curProcess.StartInfo = p; curProcess.Start(); curProcess.BeginOutputReadLine(); curProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutDataReceived); } /// <summary> /// 開始命令行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtCommand.Text.Trim())) { MessageBox.Show("請輸入命令"); } if (curBatSataus != BatStatus.ON) { curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim()); curBatSataus = BatStatus.ON; } else { MessageBox.Show("當前進程正在運行,請先關(guān)閉"); } } /// <summary> /// 結(jié)束命令行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStop_Click(object sender, EventArgs e) { if (curBatSataus == BatStatus.ON) { curProcess.CancelOutputRead();//取消異步操作 curProcess.Kill(); curBatSataus = BatStatus.OFF; //如果需要手動關(guān)閉,則關(guān)閉后再進行初始化 InitInfo(); } } /// <summary> /// 進程接受事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void ProcessOutDataReceived(object sender, DataReceivedEventArgs e) { if (this.txtOutPutInfo.InvokeRequired) { this.txtOutPutInfo.Invoke(new Action(() => { this.txtOutPutInfo.AppendText(e.Data + "\r\n"); })); } else { this.txtOutPutInfo.AppendText(e.Data + "\r\n"); } } private void timer1_Tick(object sender, EventArgs e) { if ((string.IsNullOrEmpty(this.curProcess.StartInfo.FileName) || this.curProcess.StandardInput.BaseStream.CanWrite) && curBatSataus != BatStatus.OFF) { curBatSataus = BatStatus.OFF; } } } /// <summary> /// 命令狀態(tài) /// </summary> public enum BatStatus { NONE = 0, ON = 1, OFF = 2 } }
備注:
關(guān)于如何在命令行執(zhí)行過程中【如:Ping 192.168.1.100 -t】,鍵入快捷鍵【如:Ctrl+C】等操作,目前還沒有實現(xiàn)。目前采用的就強制關(guān)閉進程方法
以上就是C# 動態(tài)輸出Dos命令執(zhí)行結(jié)果的實例(附源碼)的詳細內(nèi)容,更多關(guān)于C# 動態(tài)輸出Dos命令執(zhí)行結(jié)果的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果
webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個webservice暴露在外面可供使用的接口。今天我們來詳細討論下如何動態(tài)調(diào)用以及讀取解析返回結(jié)果2015-06-06C#泛型集合類System.Collections.Generic
這篇文章介紹了C#中的泛型集合類System.Collections.Generic,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05Quartz.Net任務和觸發(fā)器實現(xiàn)方法詳解
這篇文章主要介紹了Quartz.Net任務和觸發(fā)器實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12C#中Dictionary<TKey,TValue>排序方式的實現(xiàn)
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02C#中IEnumerable接口介紹并實現(xiàn)自定義集合
這篇文章介紹了C#中IEnumerable接口并實現(xiàn)自定義集合,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04