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();  
  15. } 
上面代码有几个不足,一是必须要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. { 
  10.     class Program 
  11.     { 
  12.         public static string cmd_str; 
  13.         public static string cmd_outstr; 
  14.  
  15.         public static Process p = new Process(); 
  16.  
  17.         static void Main(string[] args) 
  18.         { 
  19.             p.StartInfo.FileName = "cmd.exe"; 
  20.             p.StartInfo.UseShellExecute = false; 
  21.             p.StartInfo.RedirectStandardInput = true; 
  22.             p.StartInfo.RedirectStandardOutput = true; 
  23.             p.StartInfo.RedirectStandardError = true; 
  24.             p.StartInfo.CreateNoWindow = true; 
  25.             p.Start(); 
  26.  
  27.  
  28.             cmd_str = ""; 
  29.             cmd_outstr = ""; 
  30.  
  31.             Thread t1 = new Thread(new ThreadStart(DoCmdThread)); 
  32.             t1.Start(); 
  33.  
  34.             Thread t2 = new Thread(new ThreadStart(OutCmdThread)); 
  35.             t2.Start(); 
  36.  
  37.             while(true) 
  38.             { 
  39.                 cmd_str = Console.ReadLine(); 
  40.                 Thread.Sleep(10); 
  41.  
  42.                 if (cmd_str == "exit") 
  43.                     break; 
  44.             } 
  45.         } 
  46.  
  47.         public static void DoCmdThread() 
  48.         { 
  49.             while (true) 
  50.             { 
  51.                 if (cmd_str == "exit") 
  52.                     break; 
  53.                 if (cmd_str != "") 
  54.                 { 
  55.                     p.StandardInput.WriteLine(cmd_str); 
  56.                     //p.StandardInput.WriteLine("cd"); 
  57.                     cmd_str = ""; 
  58.                 } 
  59.  
  60.                 Thread.Sleep(1); 
  61.             } 
  62.         } 
  63.  
  64.         public static void OutCmdThread() 
  65.         { 
  66.             while (true) 
  67.             { 
  68.                 if (cmd_str == "exit") 
  69.                 { 
  70.                     p.StandardInput.WriteLine("exit"); 
  71.                     p.WaitForExit(); 
  72.                     p.Close(); 
  73.                     break; 
  74.                 } 
  75.                 cmd_outstr = p.StandardOutput.ReadLine(); 
  76.  
  77.                 while(cmd_outstr != "") 
  78.                 { 
  79.                     Console.WriteLine(cmd_outstr); 
  80.                     cmd_outstr = p.StandardOutput.ReadLine(); 
  81.                 } 
  82.  
  83.                 char[] ch = new char[256]; 
  84.                 int c = p.StandardOutput.Read(ch, 0, 256); 
  85.                 if (c > 0) 
  86.                 { 
  87.                    
  88.                     Console.Write(ch,0,c); 
  89.                 } 
  90.  
  91.                 Thread.Sleep(1); 
  92.             } 
  93.         } 
  94.     } 
  95. } 
posted @ 2013-10-28 09:28  AshLeakey  阅读(679)  评论(0)    收藏  举报