C#获取命令行输出内容的方法
获取命令行输出内容的方式有传统和异步两种方式。
传统方式:
public static void RunExe(string exePath, string arguments, out string output, out string error)
{
using (Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = arguments;
// 必须禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true; process.Start(); output = process.StandardOutput.ReadToEnd();
error = process.StandardError.ReadToEnd(); process.WaitForExit();
process.Close();
}
}
class Program
{
static void Main(string[] args)
{
string output;
string error;
CMD.RunExe("ping", "www.baidu.com",out output,out error);
Console.WriteLine(output +"\n"+ error);
}
}
异步方式:
/// <summary>
/// 执行一条command命令
/// </summary>
/// <param name="command">需要执行的Command</param>
/// <param name="output">输出</param>
/// <param name="error">错误</param>
public static void ExecuteCommand(string command, out string output, out string error)
{
try
{
//创建一个进程
Process process = new Process();
process.StartInfo.FileName = command; // 必须禁用操作系统外壳程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true; //启动进程
process.Start(); //准备读出输出流和错误流
string outputData = string.Empty;
string errorData = string.Empty;
process.BeginOutputReadLine();
process.BeginErrorReadLine(); process.OutputDataReceived += (sender, e) =>
{
outputData += (e.Data + "\n");
}; process.ErrorDataReceived += (sender, e) =>
{
errorData += (e.Data + "\n");
}; //等待退出
process.WaitForExit(); //关闭进程
process.Close(); //返回流结果
output = outputData;
error = errorData;
}
catch (Exception)
{
output = null;
error = null;
}
}
C#获取命令行输出内容的方法的更多相关文章
- python获取命令行输出结果
#coding=utf-8 import os command = 'ping www.baidu.com ' #可以直接在命令行中执行的命令 r = os.popen(command) #执行该 ...
- python学习 —— 使用subprocess获取命令行输出结果
这里使用的版本:Python2 >= 2.7 对于获取命令行窗口中的输出python有一个很好用的模块:subprocess 两个简单例子: 1.获取ping命令的输出: from subpro ...
- C/C++ 程序中调用命令行命令并获取命令行输出结果
在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...
- Delphi 获取命令行输出的函数
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string; var SA: TSecurityAttribute ...
- python 获取命令行输出结果
status, output = commands.getstatusoutput("sudo rm -rf a.txt") if(not status): print(" ...
- python获取命令行参数的方法(汇总)
介绍python获取命令行参数的方法:getopt模和argparse模块. python版本:2.7 一.getopt模块 主要用到了模块中的函数: options, args = getopt.g ...
- 【Python学习 】Python获取命令行参数的方法
背景 最近编写一个python程序的时候,需要去获取python命令行的参数,因此这里记录下如何获取命令行参数的方法. 一.sys 模块 在 Python 中,sys 模块是一个非常常用且十分重要的模 ...
- MFC中获取命令行参数的几种方法
在MFC程序中,可以用以下几种方法来获取命令行参数. 为方便说明,我们假设执行了命令:C:\test\app.exe -1 -2 方法一 ::GetCommandLine(); 将获取到 " ...
- WordPress 无法使用the_content()方法输出内容
在使用WordPress里在一个页面里我使用the_content()方法来输出当前页面的内容,但却显示为空,而标题,url等都没有问题 在网络上好像遇到这种情况的人很少只找到了一个说是可能是func ...
随机推荐
- iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)
原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...
- JavaScript数组:增删改查、排序等
直接上代码 // 数组应用 var peoples = ["Jack","Tom","William","Tod",&q ...
- flex align-content中的描述的“多根轴线的对齐方式”中的“多根轴线”到底是什么
flex 有两条轴线,根据flex-flow 设置的来判断的,水平为主轴的话,那么值为row,垂直为主轴的话那么为column: 其中设置align-items 和 align-content都是来设 ...
- Python 字符串
Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...
- C#中TransactionScope的使用方法和原理
在.net 1.1的时代,还没有TransactionScope类,因此很多关于事务的处理,都交给了SqlTransaction和SqlConnection,每个Transaction是基于每个Con ...
- 微信小程序入门正确姿势(一)
[未经作者本人同意,请勿以任何形式转载] >>>前言 这是 [认真学编程] 系列的 第4篇 文章(微信小程序入门系列),欢迎点赞分享.写留言,这些都是对我最好的支持. 本系列适合有一 ...
- MySQL 常用的sql语句小结(待续)
mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
前言: 找工作时(IT行业),除了常见的软件开发以外,机器学习岗位也可以当作是一个选择,不少计算机方向的研究生都会接触这个,如果你的研究方向是机器学习/数据挖掘之类,且又对其非常感兴趣的话,可以考虑考 ...
- 读书笔记---PMBOK第五版官方中文版
以下是为了准备PMP考试时学习<PMBOK第五版官方中文版>这本书的笔记和摘要,目的是为了以后可以快速的抓住本书的核心重点复习. 引论 PMPOK的目的 收录了项目管理知识体系中被普遍认可 ...
- 使用mailx发送邮件
转载:http://www.cnblogs.com/softwaretesting/archive/2011/11/23/2260520.html http://www.cnblogs.com/sof ...