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

传统方式:

 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的各种隐藏显现动画的区别

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

  2. iTextSharp生成pdf的一个简单例子

    效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...

  3. 写Java也得了解CPU--伪共享

    第一次接触伪共享的概念,是在马丁的博客上:而ifeve也把这一系列博文翻译整理好了.概读了几次,感觉到此概念的重要.因此有了这个系列的第二篇读后总结. 1. 什么是伪共享(False sharing) ...

  4. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  5. 了解 JS 原型

    原型概念 当创建了一个函数时,就会根据一组特定的规则为该函数创建一个 prototype 属性,这个属性指向函数的原型对象.在默认情况下,所有原型对象都会自动获得一个constructor 的属性 这 ...

  6. 快速创建 IEqualityComparer 实例:改进

    两年前,我写了篇文章<快速创建 IEqualityComparer<T> 和 IComparer<T> 的实例>,文中给出了一个用于快速创建 IEqualityCo ...

  7. 用VS开发PHP扩展

    开发前准备工作: VS(我用的2013) Cygwin(下载地址:http://www.cygwin.com/) 搭载了php运行环境的IIS7.5 (用来测试) php编译后的程序和编译前的源码,我 ...

  8. BZOJ 1078: [SCOI2008]斜堆

    1078: [SCOI2008]斜堆 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 770  Solved: 422[Submit][Status][ ...

  9. Python【第四章】:socket

    ocket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Uni ...

  10. MySQL基本命令

    1.修改root用户登录密码: [root@17track bin]# /usr/local/mysql/bin/mysqladmin -u root password 'MyPassword' my ...