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 ...
随机推荐
- [CTSC1999]【网络流24题】星际转移
Description 由于人类对自然资源的消耗,人们意识到大约在2300 年之后,地球就不能再居住了.于是在月球上建立了新的绿地,以便在需要时移民.令人意想不到的是,2177 年冬由于未知的原因,地 ...
- selenium IDE的3种下载安装方式
第一种方式: 打开firefox浏览器-----点击右上角-----附加组件----插件----搜索框输入“selenium”-----搜索的结果中下拉到页面尾部,点击“查看全部的37项结果”---进 ...
- cctype学习
#include <cctype>(转,归纳很好) 头文件描述: 这是一个拥有许多字符串处理函数声明的头文件,这些函数可以用来对单独字符串进行分类和转换: 其中的函数描述: 这些函数传入一 ...
- 详解MySQL存储过程的“异常处理”
阅读目录:存储过程的异常处理 定义异常处理 单一异常处理程序 continue exit 多个异常处理程序 关于错误编号和SQLSTATE码 使用3个处理程序 忽略某一异常的处理 异常处理的命名 异常 ...
- PHP的学习记录
这是我的第一次写博客,是一个PHP的初学者,刚刚开始之前是一点儿的都不懂,现在开始通过买些书籍回来学习,废话少说,开始记录笔记吧. 函数:函数的英文名为function,也就是功能的意思,在自定义函数 ...
- 从SQL Server数据库转到Oracle数据库的数据脚本处理
在我们很多情况下的开发,为了方便或者通用性的考虑,都首先考虑SQL Server数据库进行开发,但有时候客户的生产环境是Oracle或者其他数据库,那么我们就需要把对应的数据结构和数据脚本转换为对应的 ...
- 在R中整理数据
原始数据一般分散杂乱,并含有缺失和错误值,因此在进行数据分析前首先要对数据进行整理. 一.首先,了解原始数据的结构. 可使用如下函数(归属baseR)来查看数据结构: class(dataobject ...
- lightOJ 1258 Making Huge Palindromes(KMP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1258 就是求逆串和原串的匹配长度 答案就是原串长度的2倍减去匹配长度即可 第一次我将原 ...
- [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa 220 - Othello #include<iostream ...
- Unity C# 一些关于Camera的心得!
本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6856374.html 首先,总结了下最近工作中关于摄像机漫游的功能, 脚本如下: Tran ...