网上有很多用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. namespace cmdtest 
  8. class Program 
  9. public static string cmd_str; 
  10. public static string cmd_outstr; 
  11. public static Process p = new Process(); 
  12. static void Main(string[] args) 
  13. p.StartInfo.FileName = "cmd.exe"; 
  14. p.StartInfo.UseShellExecute = false; 
  15. p.StartInfo.RedirectStandardInput = true; 
  16. p.StartInfo.RedirectStandardOutput = true; 
  17. p.StartInfo.RedirectStandardError = true; 
  18. p.StartInfo.CreateNoWindow = true; 
  19. p.Start(); 
  20. cmd_str = ""; 
  21. cmd_outstr = ""; 
  22. Thread t1 = new Thread(new ThreadStart(DoCmdThread)); 
  23. t1.Start(); 
  24. Thread t2 = new Thread(new ThreadStart(OutCmdThread)); 
  25. t2.Start(); 
  26. while(true) 
  27. cmd_str = Console.ReadLine(); 
  28. Thread.Sleep(10); 
  29. if (cmd_str == "exit") 
  30. break; 
  31. public static void DoCmdThread() 
  32. while (true) 
  33. if (cmd_str == "exit") 
  34. break; 
  35. if (cmd_str != "") 
  36. p.StandardInput.WriteLine(cmd_str); 
  37. //p.StandardInput.WriteLine("cd"); 
  38. cmd_str = ""; 
  39. Thread.Sleep(1); 
  40. public static void OutCmdThread() 
  41. while (true) 
  42. if (cmd_str == "exit") 
  43. p.StandardInput.WriteLine("exit"); 
  44. p.WaitForExit(); 
  45. p.Close(); 
  46. break; 
  47. cmd_outstr = p.StandardOutput.ReadLine(); 
  48. while(cmd_outstr != "") 
  49. Console.WriteLine(cmd_outstr); 
  50. cmd_outstr = p.StandardOutput.ReadLine(); 
  51. char[] ch = new char[256]; 
  52. int c = p.StandardOutput.Read(ch, 0, 256); 
  53. if (c > 0) 
  54. Console.Write(ch,0,c); 
  55. Thread.Sleep(1); 
  56. }

C# 调用cmd.exe的方法的更多相关文章

  1. C#程序调用cmd.exe执行命令

    代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Wi ...

  2. Java 调用cmd.exe命令

    原理:java的Runtime.getRuntime().exec(commandText)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是 ...

  3. 调用cmd.exe执行pdf的合并(pdftk.exe)

    今天调查一个pdf文件的抽取,合并功能,用到下面这个工具(pdftk): https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ 在cmd.exe里执 ...

  4. C++程序中调用其他exe可执行文件方法

    在编程过程中有个需求,点击某个按钮需要弹出系统的声音控制面板.在网上查了下代码中调用其他exe程序或者打开其他文件的方法. 自己借鉴网上的文章稍微总结下,加深下印象,也给方便自己用. 在代码中调用其他 ...

  5. Qt之启动外部程序(调用cmd.exe ping putty winscp 管道等等,比较牛叉)

    http://blog.csdn.net/u011012932/article/details/50478833

  6. 在IIS7.5中ASP.NET调用cmd程序拒绝访问决绝方法小记

    前言 昨天利用Github的Webhook实现自动部署站点,其中要调用命令行(cmd.exe)程序执行shell脚本. 在本地测试没有任何问题,部署到服务器之后,发现错误信息:访问拒绝. 问题 没有权 ...

  7. C# 调用cmd命令行路径中带空格问题

    今天打包winform程序,程序中本身有一处需要调用cmd.exe,打包安装在C:\Program Files目录下,然后调用cmd的地方,就弹出了C:\Program不是内部或外部命令,也不是可运行 ...

  8. C#程序调用cmd执行命令

    对于C#通过程序来调用cmd命令的操作,网上有很多类似的文章,但很多都不行,竟是漫天的拷贝.我自己测试整理了一下. 代码: string str = Console.ReadLine(); Syste ...

  9. C#程序调用cmd执行命令(转)

    C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnos ...

随机推荐

  1. [进程通信] Linux进程间通信(IPC)

    简介 linux下进程间通信的几种主要手段: 1.      管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道 ...

  2. Android自定义评分控件:RatingStarView

    RatingStarView Android自定义的评分控件,类似ProgressBar那样的,使用星星图标(full.half.empty)作为progress标识的评分/打分控件. 效果图 图1: ...

  3. IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView

    UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...

  4. mysql的一些使用操作

    1:如果空闲时间很长导致mysql报错: Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last pa ...

  5. 转csdn-css4

    http://www.csdn.net/article/2015-03-06/2824127-css4

  6. c# 内存的具体表现- 通用类型系统 深拷贝 浅拷贝 函数传参

    c# 通用类型系统 及变量在 深拷贝 浅拷贝 函数传参 中的深层次的表现 在编程中遇到了一些想不到的异常,跟踪发现,自己对于c#变量在内存上的表现理解有偏差,系统的学习并通过代码实验梳理了各种情况下, ...

  7. Haproxy------在windows下配置负载均衡

    配置Haproxy 1.解压Haproxy到d:\haproxy 2.置haproxy.cfg文件 global log 127.0.0.1 local0 maxconn 1500 daemon de ...

  8. PHP 中级内容

    1.面向对象编程(OOP) 2.模板引擎(smarty) 3.MYSQL(中级操作) 数据库抽象层(PDO): 4.Ajax(异步刷新) 5.Js框架(jQuery) Xml+JSON数据       ...

  9. Java并发编程:Callable、Future和FutureTask的实现

    启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...

  10. VS2010 使用 EF5 框架步骤

    1.安装 nuget , 在nuget里联机查找 EF 5.x DbContext Generator for C# 模版下载安装 2.nuget 控制台执行 Install-Package Enti ...