C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)
本文以一個(gè)簡(jiǎn)單的小例子講解如何將命令行信息實(shí)時(shí)的輸出到文本框中。僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。
概述
在C#程序開(kāi)發(fā)過(guò)程中,有時(shí)需要運(yùn)行其它的程序并獲得輸出的結(jié)果來(lái)進(jìn)行進(jìn)一步的處理。一般第三方的程序,主要通過(guò)進(jìn)程來(lái)調(diào)用,如果能夠獲取第三方程序執(zhí)行過(guò)程中的信息,就顯得方便而有用。
涉及知識(shí)點(diǎn):
- 進(jìn)程相關(guān)類: Process,ProcessStartInfo,主要設(shè)置進(jìn)程的重定向輸出,以及接受數(shù)據(jù)的事件。
- 文本框操作:多行顯示,滾動(dòng)條總在最下面
效果圖
如下【如果命令執(zhí)行完畢,會(huì)自動(dòng)結(jié)束,如果中斷進(jìn)程,可以手動(dòng)點(diǎn)擊結(jié)束進(jìn)程】:

核心代碼
主要代碼如下:
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>
/// 開(kāi)始命令行
/// </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("請(qǐng)輸入命令");
}
if (curBatSataus != BatStatus.ON)
{
curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim());
curBatSataus = BatStatus.ON;
}
else {
MessageBox.Show("當(dāng)前進(jìn)程正在運(yùn)行,請(qǐng)先關(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;
//如果需要手動(dòng)關(guān)閉,則關(guān)閉后再進(jìn)行初始化
InitInfo();
}
}
/// <summary>
/// 進(jìn)程接受事件
/// </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í)行過(guò)程中【如:Ping 192.168.1.100 -t】,鍵入快捷鍵【如:Ctrl+C】等操作,目前還沒(méi)有實(shí)現(xiàn)。目前采用的就強(qiáng)制關(guān)閉進(jìn)程方法
以上就是C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
動(dòng)態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果
webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來(lái)發(fā)布的,在WSDL文件里面,包含這個(gè)webservice暴露在外面可供使用的接口。今天我們來(lái)詳細(xì)討論下如何動(dòng)態(tài)調(diào)用以及讀取解析返回結(jié)果2015-06-06
C#泛型集合類System.Collections.Generic
這篇文章介紹了C#中的泛型集合類System.Collections.Generic,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Quartz.Net任務(wù)和觸發(fā)器實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Quartz.Net任務(wù)和觸發(fā)器實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn)
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C#實(shí)現(xiàn)系統(tǒng)休眠或靜止休眠的方法
這篇文章主要介紹了C#實(shí)現(xiàn)系統(tǒng)休眠或靜止休眠的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
C#基于Modbus三種CRC16校驗(yàn)方法的性能對(duì)比
這篇文章主要介紹了C#基于Modbus三種CRC16校驗(yàn)方法的性能對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
C#中IEnumerable接口介紹并實(shí)現(xiàn)自定義集合
這篇文章介紹了C#中IEnumerable接口并實(shí)現(xiàn)自定義集合,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
解析C# Console 控制臺(tái)為什么也會(huì)卡死(原因分析)
在分析旅程中,總會(huì)有幾例控制臺(tái)的意外卡死導(dǎo)致的生產(chǎn)事故,有經(jīng)驗(yàn)的朋友都知道,控制臺(tái)卡死一般是動(dòng)了快速編輯窗口的緣故,雖然知道緣由,但一直沒(méi)有時(shí)間探究底層原理,市面上也沒(méi)有對(duì)這塊的底層原理介紹,昨天花了點(diǎn)時(shí)間簡(jiǎn)單探究了下,感興趣的朋友一起看看吧2023-10-10

