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 ...
随机推荐
- Linux Bootup Time
Linux Bootup Time 英文原文地址:http://elinux.org/Boot_Time 1. 简介 启动时间这一话题包括很多子话题,比如启动时间的衡量.启动时间的分析.人为因素分 ...
- c#常用方法和类
1. 数据类型转换函数 Convert.ToXXX(); XXX.Parse(); XXX.TryParse(); 2. 日期相关的类与函数 获取系统当前日期(含时间):DateTime.Now 获 ...
- [大数据]-Elasticsearch5.3.1+Kibana5.3.1从单机到分布式的安装与使用<2>
前言:上篇[大数据]-Elasticsearch5.3.1+Kibana5.3.1从单机到分布式的安装与使用<1>中介绍了ES ,Kibana的单机到分布式的安装,这里主要是介绍Elast ...
- Mqtt服务器搭建
.bg { background: #99CC99 } Mqtt服务器搭建 测试环境:CentOS64位 1.安装基础软件 yum install gcc-c++ yum install cmake ...
- ZooKeeper 入门
0 介绍 官网:http://zookeeper.apache.org/ ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分 ...
- [刷题]Google Code Jam 2017 - Round1 C Problem A. Ample Syrup
https://code.google.com/codejam/contest/3274486/dashboard Problem The kitchen at the Infinite House ...
- 基本排序算法<二>
归并排序 归并排序,顾名思义,就是通过将两个有序的序列合并为一个大的有序的序列的方式来实现排序.合并排序是一种典型的分治算法:首先将序列分为两部分,然后对每一部分进行循环递归的排序,然后逐个将结果进行 ...
- java中一个重要思想:面向对象
面向对象: 1, 面向过程的思想(合适的方法出现在合适的类里面) 准备去一个地方: 先买车, 挂牌, 开导航, 踩油门, 过黄河, 穿越珠穆朗玛峰... 2, 面向对象的思想 我开着车去, 车怎么去随 ...
- 如何优雅地运用 Chrome (Google)
已经在很多工具类文章前言中,提及使用工具的重要性:以至于在写这篇时候,大为窘迫:穷尽了脑海中那些名句箴言,目测都已然在先前文章中被引用.鉴于杳让人心底意识到工具的重要性,并且能践行之,远比介绍如何使用 ...
- C语言实验单片机串口发送int型数据
void SendIint(int n)reentrant { unsigned char s; while(n!=0) { s=(unsigned char)n%10+48; SendByte(s) ...