最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例(附源碼)

 更新時(shí)間:2020年07月13日 09:24:52   作者:Alan.hsiang  
這篇文章主要介紹了C# 動(dòng)態(tài)輸出Dos命令執(zhí)行結(jié)果的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

本文以一個(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)文章

最新評(píng)論

盐津县| 黎平县| 文昌市| 卓资县| 绥宁县| 罗山县| 嘉善县| 灌南县| 扬中市| 鄂托克旗| 南安市| 龙胜| 临洮县| 泸定县| 从化市| 辽宁省| 白玉县| 文成县| 邢台市| 禄丰县| 永登县| 枞阳县| 思茅市| 华安县| 绵竹市| 灵武市| 四平市| 安庆市| 墨玉县| 双城市| 万安县| 通城县| 丹巴县| 双峰县| 临邑县| 满洲里市| 盐城市| 通州市| 岳西县| 山阴县| 金寨县|