C# 调用cmd.exe的方法

网上有很多用C#调用cmd的方法,大致如下:

[c-sharp] view plaincopy

  1. private void ExecuteCmd(string command)  
  2. {  
  3. Process p = new Process();  
  4. p.StartInfo.FileName = "cmd.exe";  
  5. p.StartInfo.UseShellExecute = false;  
  6. p.StartInfo.RedirectStandardInput = true;  
  7. p.StartInfo.RedirectStandardOutput = true;  
  8. p.StartInfo.CreateNoWindow = true;  
  9. p.Start();  
  10. p.StandardInput.WriteLine(command);  
  11. p.StandardInput.WriteLine("exit");  
  12. p.WaitForExit();  
  13. this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd();  
  14. p.Close();  
上面代码有几个不足,一是必须要exit那一句,否则就会死循环。


再就是每次执行Execute执行cmd后,都必须等到cmd执行完且cmd.exe进程退出,才能读到结果。有时候这样会让
我们的应用程序失去操作的连续性。
事实上,通过两个线程,一个访问输入管道,一个访问输出管道,可以很容易实现持续性的效果,
下面是一个Console程序:

[c-sharp] view plaincopy

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading; 
  6. using System.Diagnostics; 
  7.  
  8. namespace cmdtest 
  9.     class Program 
  10.     { 
  11.         public static string cmd_str; 
  12.         public static string cmd_outstr; 
  13.  
  14.         public static Process p = new Process(); 
  15.  
  16.         static void Main(string[] args) 
  17.         { 
  18.             p.StartInfo.FileName = "cmd.exe"; 
  19.             p.StartInfo.UseShellExecute = false; 
  20.             p.StartInfo.RedirectStandardInput = true; 
  21.             p.StartInfo.RedirectStandardOutput = true; 
  22.             p.StartInfo.RedirectStandardError = true; 
  23.             p.StartInfo.CreateNoWindow = true; 
  24.             p.Start(); 
  25.  
  26.  
  27.             cmd_str = ""; 
  28.             cmd_outstr = ""; 
  29.  
  30.             Thread t1 = new Thread(new ThreadStart(DoCmdThread)); 
  31.             t1.Start(); 
  32.  
  33.             Thread t2 = new Thread(new ThreadStart(OutCmdThread)); 
  34.             t2.Start(); 
  35.  
  36.             while(true) 
  37.             { 
  38.                 cmd_str = Console.ReadLine(); 
  39.                 Thread.Sleep(10); 
  40.  
  41.                 if (cmd_str == "exit") 
  42.                     break; 
  43.             } 
  44.         } 
  45.  
  46.         public static void DoCmdThread() 
  47.         { 
  48.             while (true) 
  49.             { 
  50.                 if (cmd_str == "exit") 
  51.                     break; 
  52.                 if (cmd_str != "") 
  53.                 { 
  54.                     p.StandardInput.WriteLine(cmd_str); 
  55.                     //p.StandardInput.WriteLine("cd"); 
  56.                     cmd_str = ""; 
  57.                 } 
  58.  
  59.                 Thread.Sleep(1); 
  60.             } 
  61.         } 
  62.  
  63.         public static void OutCmdThread() 
  64.         { 
  65.             while (true) 
  66.             { 
  67.                 if (cmd_str == "exit") 
  68.                 { 
  69.                     p.StandardInput.WriteLine("exit"); 
  70.                     p.WaitForExit(); 
  71.                     p.Close(); 
  72.                     break; 
  73.                 } 
  74.                 cmd_outstr = p.StandardOutput.ReadLine(); 
  75.  
  76.                 while(cmd_outstr != "") 
  77.                 { 
  78.                     Console.WriteLine(cmd_outstr); 
  79.                     cmd_outstr = p.StandardOutput.ReadLine(); 
  80.                 } 
  81.  
  82.                 char[] ch = new char[256]; 
  83.                 int c = p.StandardOutput.Read(ch, 0, 256); 
  84.                 if (c > 0) 
  85.                 { 
  86.                    
  87.                     Console.Write(ch,0,c); 
  88.                 } 
  89.  
  90.                 Thread.Sleep(1); 
  91.             } 
  92.         } 
  93.     } 
posted @ 2013-10-28 09:28  AshLeakey  阅读(641)  评论(0编辑  收藏  举报