C# WinForm程序来执行控制台命令的源代码
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
C# WinForm命令行[coonmajia]的用法演示,使用WinForm程序来执行控制台命令的源代码,将CMD可视化,变成Windows窗体的形式,究竟是如何实现的呢?看一下源码就知道了。相关代码片断:
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执行控制台命令 { 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 编辑过 |
关键字查询
相关文章
正在查询... |