C# 调用cmd.exe的方法
网上有很多用C#调用cmd的方法,大致如下:
[c-sharp] view plaincopy
- private void ExecuteCmd(string command)
- {
- Process p = new Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- p.StandardInput.WriteLine(command);
- p.StandardInput.WriteLine("exit");
- p.WaitForExit();
- this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd();
- p.Close();
- }
上面代码有几个不足,一是必须要exit那一句,否则就会死循环。 再就是每次执行Execute执行cmd后,都必须等到cmd执行完且cmd.exe进程退出,才能读到结果。有时候这样会让我们的应用程序失去操作的连续性。
事实上,通过两个线程,一个访问输入管道,一个访问输出管道,可以很容易实现持续性的效果,
下面是一个Console程序:
[c-sharp] view plaincopy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Diagnostics;
- namespace cmdtest
- {
- class Program
- {
- public static string cmd_str;
- public static string cmd_outstr;
- public static Process p = new Process();
- static void Main(string[] args)
- {
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- cmd_str = "";
- cmd_outstr = "";
- Thread t1 = new Thread(new ThreadStart(DoCmdThread));
- t1.Start();
- Thread t2 = new Thread(new ThreadStart(OutCmdThread));
- t2.Start();
- while(true)
- {
- cmd_str = Console.ReadLine();
- Thread.Sleep(10);
- if (cmd_str == "exit")
- break;
- }
- }
- public static void DoCmdThread()
- {
- while (true)
- {
- if (cmd_str == "exit")
- break;
- if (cmd_str != "")
- {
- p.StandardInput.WriteLine(cmd_str);
- //p.StandardInput.WriteLine("cd");
- cmd_str = "";
- }
- Thread.Sleep(1);
- }
- }
- public static void OutCmdThread()
- {
- while (true)
- {
- if (cmd_str == "exit")
- {
- p.StandardInput.WriteLine("exit");
- p.WaitForExit();
- p.Close();
- break;
- }
- cmd_outstr = p.StandardOutput.ReadLine();
- while(cmd_outstr != "")
- {
- Console.WriteLine(cmd_outstr);
- cmd_outstr = p.StandardOutput.ReadLine();
- }
- char[] ch = new char[256];
- int c = p.StandardOutput.Read(ch, 0, 256);
- if (c > 0)
- {
- Console.Write(ch,0,c);
- }
- Thread.Sleep(1);
- }
- }
- }
- }
C# 调用cmd.exe的方法的更多相关文章
- C#程序调用cmd.exe执行命令
代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Wi ...
- Java 调用cmd.exe命令
原理:java的Runtime.getRuntime().exec(commandText)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是 ...
- 调用cmd.exe执行pdf的合并(pdftk.exe)
今天调查一个pdf文件的抽取,合并功能,用到下面这个工具(pdftk): https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ 在cmd.exe里执 ...
- C++程序中调用其他exe可执行文件方法
在编程过程中有个需求,点击某个按钮需要弹出系统的声音控制面板.在网上查了下代码中调用其他exe程序或者打开其他文件的方法. 自己借鉴网上的文章稍微总结下,加深下印象,也给方便自己用. 在代码中调用其他 ...
- Qt之启动外部程序(调用cmd.exe ping putty winscp 管道等等,比较牛叉)
http://blog.csdn.net/u011012932/article/details/50478833
- 在IIS7.5中ASP.NET调用cmd程序拒绝访问决绝方法小记
前言 昨天利用Github的Webhook实现自动部署站点,其中要调用命令行(cmd.exe)程序执行shell脚本. 在本地测试没有任何问题,部署到服务器之后,发现错误信息:访问拒绝. 问题 没有权 ...
- C# 调用cmd命令行路径中带空格问题
今天打包winform程序,程序中本身有一处需要调用cmd.exe,打包安装在C:\Program Files目录下,然后调用cmd的地方,就弹出了C:\Program不是内部或外部命令,也不是可运行 ...
- C#程序调用cmd执行命令
对于C#通过程序来调用cmd命令的操作,网上有很多类似的文章,但很多都不行,竟是漫天的拷贝.我自己测试整理了一下. 代码: string str = Console.ReadLine(); Syste ...
- C#程序调用cmd执行命令(转)
C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnos ...
随机推荐
- 机器学习笔记-1 Linear Regression with Multiple Variables(week 2)
1. Multiple Features note:X0 is equal to 1 2. Feature Scaling Idea: make sure features are on a simi ...
- 博弈论(Game Theory) - 04 - 纳什均衡
博弈论(Game Theory) - 04 - 纳什均衡 开始 纳什均衡和最大最小定理是博弈论的两大基石. 博弈不仅仅是对抗,也包括合作和迁就,纳什均衡能够解决这些问题,提供了在数学上一个完美的理论. ...
- iOS 制作自动打包脚本 Xcode8.3.2
本文包含以下内容: 前言 1.shell脚本的编写 2.xcodebuild命令 3.完整的可用示例 参考资料 前言 做iOS开发,打包APP是比较频繁的事情,每次都手动去配置一堆东西确实是比较乏味. ...
- tail命令
tail命令用来取文件后几行,默认显示后10行.有多个FILE,每个都带有一个头文件名称. 语法: tail [OPTION]... [FILE]... 选项: -n#:取文件后#行,n可省略: -c ...
- 支付宝app支付服务器签名代码(C#)
1,引入支付宝的sdk(AopSdk) 支付宝接口文档网站可下载,注意下载C#版本: 2,代码写的比较简单 public static string RSASign(string OrderNo,de ...
- Shell脚本编写
1.什么是Shell脚本 Shell脚本是利用 shell 的功能所写的一个程序 program,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式.管 ...
- ZooKeeper实践:(1)配置管理
一. 前言 配置是每个程序不可或缺的一部分,配置有多重方式:xml.ini.property.database等等,从最初的单机环境到现在的分布式环境. 1. 以文件的格式存储配置,修改任何都 ...
- jQuery选择器的的优点
jQuery选择器的的优点 选择器想必大家都不陌生,今天呢,我就给大家介绍一下jQuery选择器的优点: jQuery选择器更简洁的写法: jQuery完善的处理机制: jQuery选择器判断dom节 ...
- 关于Cookie的知识的总结
Cookie的类型 会话cookie和持久cookie 会话cookie是一种临时cookie,它记录了用户访问站点时的设置和偏好,当用户退出浏览器时,会话cookie就会被删除. 持久cookie的 ...
- JVM、GC与HashMap
阿里巴巴突然来了个面试邀请电话,问了些java底层的东西,不知所措,所以专门花了些时间做了下学习,顺便记录下,好记性不如烂笔头. 一.对JAVA的垃圾回收机制(GC)的理解 不同于C/C++需要手工释 ...