C# Process运行cmd命令的异步回显

来源:互联网 发布:php的框架有哪些 编辑:程序博客网 时间:2024/06/07 05:59

以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子:

 

 

 

 

 

 

 

 

 

 

 

 

 

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Diagnostics;  
  10.   
  11. namespace CmdCallbackShow  
  12. {  
  13.     // 1.定义委托  
  14.     public delegate void DelReadStdOutput(string result);  
  15.     public delegate void DelReadErrOutput(string result);  
  16.   
  17.     public partial class Form1 : Form  
  18.     {  
  19.         // 2.定义委托事件  
  20.         public event DelReadStdOutput ReadStdOutput;  
  21.         public event DelReadErrOutput ReadErrOutput;  
  22.   
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.             Init();  
  27.         }  
  28.   
  29.         private void Init()  
  30.         {  
  31.             //3.将相应函数注册到委托事件中  
  32.             ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction);  
  33.             ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction);  
  34.         }  
  35.   
  36.         private void button1_Click(object sender, EventArgs e)  
  37.         {  
  38.             // 启动进程执行相应命令,此例中以执行ping.exe为例  
  39.             RealAction("ping.exe", textBox1.Text);  
  40.         }  
  41.           
  42.         private void RealAction(string StartFileName, string StartFileArg)  
  43.         {  
  44.             Process CmdProcess = new Process();  
  45.             CmdProcess.StartInfo.FileName = StartFileName;      // 命令  
  46.             CmdProcess.StartInfo.Arguments = StartFileArg;      // 参数  
  47.   
  48.             CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口  
  49.             CmdProcess.StartInfo.UseShellExecute = false;  
  50.             CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入  
  51.             CmdProcess.StartInfo.RedirectStandardOutput = true// 重定向标准输出  
  52.             CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
  53.             //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
  54.   
  55.             CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);  
  56.             CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);  
  57.   
  58.             CmdProcess.EnableRaisingEvents = true;                      // 启用Exited事件  
  59.             CmdProcess.Exited += new EventHandler(CmdProcess_Exited);   // 注册进程结束事件  
  60.   
  61.             CmdProcess.Start();  
  62.             CmdProcess.BeginOutputReadLine();  
  63.             CmdProcess.BeginErrorReadLine();  
  64.   
  65.             // 如果打开注释,则以同步方式执行命令,此例子中用Exited事件异步执行。  
  66.             // CmdProcess.WaitForExit();       
  67.         }  
  68.   
  69.         private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)  
  70.         {  
  71.             if (e.Data != null)  
  72.             {  
  73.                 // 4. 异步调用,需要invoke  
  74.                 this.Invoke(ReadStdOutput, new object[] { e.Data });  
  75.             }  
  76.         }  
  77.   
  78.         private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)  
  79.         {  
  80.             if (e.Data != null)  
  81.             {  
  82.                 this.Invoke(ReadErrOutput, new object[] { e.Data });  
  83.             }  
  84.         }  
  85.   
  86.         private void ReadStdOutputAction(string result)  
  87.         {  
  88.             this.textBoxShowStdRet.AppendText(result + "\r\n");  
  89.         }  
  90.   
  91.         private void ReadErrOutputAction(string result)  
  92.         {  
  93.             this.textBoxShowErrRet.AppendText(result + "\r\n");  
  94.         }  
  95.   
  96.         private void CmdProcess_Exited(object sender, EventArgs e)  
  97.         {  
  98.             // 执行结束后触发  
  99.         }  
  100.     }  
  101. }  

原文转自:http://blog.csdn.net/irwin_chen/article/details/7430551
 

原创粉丝点击