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

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

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

本文以一個簡單的小例子講解如何將命令行信息實時的輸出到文本框中。僅供學習分享使用,如有不足之處,還請指正。

概述

在C#程序開發(fā)過程中,有時需要運行其它的程序并獲得輸出的結果來進行進一步的處理。一般第三方的程序,主要通過進程來調用,如果能夠獲取第三方程序執(zhí)行過程中的信息,就顯得方便而有用。

涉及知識點:

  • 進程相關類: Process,ProcessStartInfo,主要設置進程的重定向輸出,以及接受數(shù)據(jù)的事件。
  • 文本框操作:多行顯示,滾動條總在最下面

效果圖

如下【如果命令執(zhí)行完畢,會自動結束,如果中斷進程,可以手動點擊結束進程】:

核心代碼

主要代碼如下:

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("當前進程正在運行,請先關閉");
   }

  }

  /// <summary>
  /// 結束命令行
  /// </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;
    //如果需要手動關閉,則關閉后再進行初始化
    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
 }
}

備注:

關于如何在命令行執(zhí)行過程中【如:Ping 192.168.1.100 -t】,鍵入快捷鍵【如:Ctrl+C】等操作,目前還沒有實現(xiàn)。目前采用的就強制關閉進程方法

源碼下載

以上就是C# 動態(tài)輸出Dos命令執(zhí)行結果的實例(附源碼)的詳細內容,更多關于C# 動態(tài)輸出Dos命令執(zhí)行結果的資料請關注腳本之家其它相關文章!

相關文章

最新評論

九江县| 湟源县| 乌鲁木齐市| 万载县| 郸城县| 湘乡市| 化隆| 花莲市| 大洼县| 金门县| 梁平县| 名山县| 禹州市| 招远市| 含山县| 乐安县| 迁西县| 黔南| 清涧县| 枝江市| 富阳市| 龙胜| 沽源县| 府谷县| 浦县| 兖州市| 昔阳县| 苗栗县| 黔西县| 山东省| 新和县| 陇川县| 沭阳县| 华蓥市| 芷江| 滨海县| 宾阳县| 静海县| 博爱县| 巴塘县| 汾阳市|