获取命令行输出内容的方式有传统和异步两种方式。

传统方式:

 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#获取命令行输出内容的方法的更多相关文章

  1. python获取命令行输出结果

    #coding=utf-8 import os   command = 'ping www.baidu.com ' #可以直接在命令行中执行的命令 r = os.popen(command) #执行该 ...

  2. python学习 —— 使用subprocess获取命令行输出结果

    这里使用的版本:Python2 >= 2.7 对于获取命令行窗口中的输出python有一个很好用的模块:subprocess 两个简单例子: 1.获取ping命令的输出: from subpro ...

  3. C/C++ 程序中调用命令行命令并获取命令行输出结果

    在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果.例如system(“ls”)只能得到0或非0,如果要获得ls的执行结 ...

  4. Delphi 获取命令行输出的函数

    function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string; var SA: TSecurityAttribute ...

  5. python 获取命令行输出结果

    status, output = commands.getstatusoutput("sudo rm -rf a.txt") if(not status): print(" ...

  6. python获取命令行参数的方法(汇总)

    介绍python获取命令行参数的方法:getopt模和argparse模块. python版本:2.7 一.getopt模块 主要用到了模块中的函数: options, args = getopt.g ...

  7. 【Python学习 】Python获取命令行参数的方法

    背景 最近编写一个python程序的时候,需要去获取python命令行的参数,因此这里记录下如何获取命令行参数的方法. 一.sys 模块 在 Python 中,sys 模块是一个非常常用且十分重要的模 ...

  8. MFC中获取命令行参数的几种方法

    在MFC程序中,可以用以下几种方法来获取命令行参数. 为方便说明,我们假设执行了命令:C:\test\app.exe -1 -2 方法一 ::GetCommandLine(); 将获取到 " ...

  9. WordPress 无法使用the_content()方法输出内容

    在使用WordPress里在一个页面里我使用the_content()方法来输出当前页面的内容,但却显示为空,而标题,url等都没有问题 在网络上好像遇到这种情况的人很少只找到了一个说是可能是func ...

随机推荐

  1. jquery的回调对象Callbacks详解

    Callbacks : 对函数的统一管理 Callbacks的options参数接受4个属性,分别是once : 只执行一次momery : 记忆stopOnFalse : 强制退出循环unique ...

  2. CWMP开源代码研究2——easycwmp安装和学习

    声明:本文是对开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文所有笔记和代码可以到csdn下载:http://download.csdn.n ...

  3. 15-前端开发之JavaScript

    什么是 JavaScript ? JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出 ...

  4. ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程

    环境:OS X 10.10.5 + JDK 1.8 步骤: 一.下载ELK的三大组件 Elasticsearch下载地址: https://www.elastic.co/downloads/elast ...

  5. 【ASP.NET实战教程】ASP.NET实战教程大集合,各种项目实战集合

    [ASP.NET实战教程]ASP.NET实战教程大集合,各种项目实战集合,希望大家可以好好学习教程中,有的比较老了,但是一直很经典!!!!论坛中很多小伙伴说.net没有实战教程学习,所以小编连夜搜集整 ...

  6. ubuntu14.04 python3.*连接mysql

    先下载pymysql文件,http://webscripts.softpedia.com/script/Database-Tools/PyMySQL-71606.html 我下载的是:PyMySQL- ...

  7. CSS div水平垂直居中和div置于底部

    一.水平居中 .hor_center { margin: 0 auto; } 二.水平垂直居中 .content { width: 360px; height: 240px; } .ver_hor_c ...

  8. LinuxMint 18 编译cm13.0 笔记

    1.安装依赖文件 sudo apt--dev libesd0-dev git-core gnupg flex bison gperf build-essential zip curl zlib1g-d ...

  9. android开发之背景音乐与音效

    android开发之背景音乐与音效 一:添加背景音乐(MediaPlayer) MediaPlayer class can be used to control playback of audio/v ...

  10. C#的继承

    什么是继承:继承是允许重用现有类去创建新类的过程.分类的原则是一个类派生出来的子类具有这个类的所有非私有的属性. 1.继承C#中的类:C#不支持多重继承,C#类始终继承自一个基类(如果未在声明中指定一 ...