C#隐式执行CMD命令
本文实现C#隐式执行CMD功能命令。下图是示例程序的主界面。
在命令文本框输入DOS命令,点击“Run”button。在以下的文本框中输出执行结果。
以下是程序的完整代码。
本程序没有使用p.StandardOutput.ReadtoEnd()和p.StandardOutput.ReadLine()方法来获得输出,由于这些方法运行后画面easy卡死。
而是通过调用异步方法BeginOutputReadLine来获取输出。并在事件p.OutputDataReceived的事件处理方法中来处理结果。
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms; namespace RunDosCommandForm
{
publicpartialclassForm1 : Form
{
publicForm1()
{
InitializeComponent();
} privatevoidbutton1_Click(object sender, EventArgse)
{
ExcuteDosCommand(textBox1.Text);
} privatevoidExcuteDosCommand(string cmd)
{
try
{
Process p = newProcess();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += newDataReceivedEventHandler(sortProcess_OutputDataReceived);
p.Start();
StreamWriter cmdWriter = p.StandardInput;
p.BeginOutputReadLine();
if (!String.IsNullOrEmpty(cmd))
{
cmdWriter.WriteLine(cmd);
}
cmdWriter.Close();
p.WaitForExit();
p.Close();
}
catch(Exception ex)
{
MessageBox.Show("运行命令失败,请检查输入的命令是否正确!");
}
} privatevoidsortProcess_OutputDataReceived(object sender,DataReceivedEventArgs e)
{
if(!String.IsNullOrEmpty(e.Data))
{
this.BeginInvoke(newAction(() => { this.listBox1.Items.Add(e.Data);}));
}
}
}
}
我们还能够将须要执行的CMD命令保存为BAT文件。再使用Process类来执行。
Process p = new Process();//设定调用的程序名,不是系统文件夹的须要完整路径
p.StartInfo.FileName = "cmd.bat";//传入运行參数
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;//是否重定向标准输入
p.StartInfo.RedirectStandardInput = false;//是否重定向标准转出
p.StartInfo.RedirectStandardOutput = false;//是否重定向错误
p.StartInfo.RedirectStandardError = false;//运行时是不是显示窗体
p.StartInfo.CreateNoWindow = true;//启动
p.Start();
p.WaitForExit();
p.Close();
版权声明:本文博主原创文章,博客,未经同意不得转载。
C#隐式执行CMD命令的更多相关文章
- C#隐式运行CMD命令(隐藏命令窗口)
原文 C#隐式运行CMD命令(隐藏命令窗口) 本文实现了C#隐式运行CMD命令的功能.下图是实例程序的主画面.在命令文本框输入DOS命令,点击"Run"按钮,在下面的文本框中输出运 ...
- C#中隐式操作CMD命令行窗口
原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...
- JAVA之执行cmd命令
感言在前:时隔好久没有更新博客园了,忙东忙西也没忙出个什么之所以然来.回首过去一两个月,只能用“疲倦”两个字来形容,而且是身心疲惫.每天11.12个小时的工作我都没觉得烦,但是总是想克服却又很难克服的 ...
- [转]Delphi执行CMD命令
今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...
- C# 执行CMD 命令
/// <summary> /// 执行CMD 命令 /// </summary> /// <param name="strCommand">命 ...
- C# 执行CMD命令的方法
/// <summary> /// 执行CMD命令 /// </summary> /// <param name="str"></para ...
- 如何使用Java执行cmd命令
用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray); ...
- java执行cmd命令并获取输出结果
1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...
- Java 调用并执行cmd命令
cmd java 调用 执行 概要: Java 调用并执行cmd命令 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
随机推荐
- POJ3692 Kindergarten 【最大独立集】
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5317 Accepted: 2589 Desc ...
- Eclipse,hadoop2.7.2 hadoop-eclipse-plugin.jar的制作
装好了hadoop后发现有装个eclipse的必要,于是参照文章A(http://www.powerxing.com/hadoop-build-project-using-eclipse/)进行安装, ...
- 内容提供者 DocumentProvider Uri工具类
Activity /**详见http://blog.csdn.net/coder_pig/article/details/47905881 Calendar Provider:日历提供者,就是针对针对 ...
- Char Tools,方便的字符编码转换小工具
工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...
- JAVA-FileInputStream之read方法
今天一个友询问FileInputStrem方法的read()和read(byte b) 方法为什么都用-1来判断读文件结束的问题,在此和大家一起学习下. 关于FileInputStream 它用于读取 ...
- 转载——SQL Server数据库性能优化之SQL语句篇
转载自:http://www.blogjava.net/allen-zhe/archive/2010/07/23/326927.html 1. 按需索取字段,跟“SELECT *”说拜拜 字段的提取一 ...
- PHP echo, print, printf, sprintf函数的区别和使用
1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bol ...
- MySQL学习笔记(3) - 查询服务器版本,当前时间,当前用户
SELECT VERSION(); --显示当前服务器版本 SELECT NOW(); --显示当前日期时间 SELECT USER(); --显示当前用户 MySQL中语句规范: 1.关键字和函数名 ...
- 3月26日html(七)window document
---恢复内容开始--- 1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: v ...
- 在PHP中PDO解决中文乱码问题
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true)); $stmt = $th ...