C# WinForm程序來執(zhí)行控制臺(tái)命令的源代碼
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
C# WinForm命令行[coonmajia]的用法演示,使用WinForm程序來執(zhí)行控制臺(tái)命令的源代碼,將CMD可視化,變成Windows窗體的形式,究竟是如何實(shí)現(xiàn)的呢?看一下源碼就知道了。相關(guān)代碼片斷:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace WinForm執(zhí)行控制臺(tái)命令 { public partial class Form1 : Form { Process p; StreamWriter input; public Form1() { InitializeComponent(); p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); p.Start(); input = p.StandardInput; p.BeginOutputReadLine(); } void p_OutputDataReceived(object sender, DataReceivedEventArgs e) { update(e.Data + Environment.NewLine); } private void button1_Click(object sender, EventArgs e) { input.WriteLine(textBox1.Text); textBox1.SelectAll(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { p.Close(); } delegate void updateDelegate(string msg); void update(string msg) { if (this.InvokeRequired) Invoke(new updateDelegate(update), new object[] { msg }); else { textBox2.Text += msg; textBox2.SelectionStart = textBox2.Text.Length - 1; textBox2.ScrollToCaret(); } } } } 該文章在 2021/2/4 16:28:08 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |