C# Process运行cmd命令的异步回显
以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 以下的代码为 new Process() 调用cmd命令,并将结果异步回显到Form的例子: [csharp] view plain copy using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace CmdCallbackShow { // 1.定义委托 public delegate void DelReadStdOutput( string result); public delegate void DelReadErrOutput( string result); public partial class Form1 : Form { // 2.定义委托事件 public event DelReadStdOutput ReadStdOutput; public event DelReadErrOutput ReadErrOutput; public Form1() { InitializeComponent(); Init(); } private void Init() { //3.将相应函数注册到委托事件中 ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction); ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction); } private void button1_Click( object sender, EventArgs e) { // 启动进程执行相应命令,此例中以执行ping.exe为例 RealAction( "ping.exe" , textBox1.Text); } private void RealAction( string StartFileName, string StartFileArg) { Process CmdProcess = new Process(); CmdProcess.StartInfo.FileName = StartFileName; // 命令 CmdProcess.StartInfo.Arguments = StartFileArg; // 参数 CmdProcess.StartInfo.CreateNoWindow = true ; // 不创建新窗口 CmdProcess.StartInfo.UseShellExecute = false ; CmdProcess.StartInfo.RedirectStandardInput = true ; // 重定向输入 CmdProcess.StartInfo.RedirectStandardOutput = true ; // 重定向标准输出 CmdProcess.StartInfo.RedirectStandardError = true ; // 重定向错误输出 //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); CmdProcess.EnableRaisingEvents = true ; // 启用Exited事件 CmdProcess.Exited += new EventHandler(CmdProcess_Exited); // 注册进程结束事件 CmdProcess.Start(); CmdProcess.BeginOutputReadLine(); CmdProcess.BeginErrorReadLine(); // 如果打开注释,则以同步方式执行命令,此例子中用Exited事件异步执行。 // CmdProcess.WaitForExit(); } private void p_OutputDataReceived( object sender, DataReceivedEventArgs e) { if (e.Data != null ) { // 4. 异步调用,需要invoke this .Invoke(ReadStdOutput, new object [] { e.Data }); } } private void p_ErrorDataReceived( object sender, DataReceivedEventArgs e) { if (e.Data != null ) { this .Invoke(ReadErrOutput, new object [] { e.Data }); } } private void ReadStdOutputAction( string result) { this .textBoxShowStdRet.AppendText(result + "\r\n" ); } private void ReadErrOutputAction( string result) { this .textBoxShowErrRet.AppendText(result + "\r\n" ); } private void CmdProcess_Exited( object sender, EventArgs e) { // 执行结束后触发 } } } |
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2011-09-08 在解析C#结构体指针前,必须知道C#结构体是如何定义的。在c#中同样定义该结构体。
2011-09-08 C++与C#内存管理对比分析 (转载)