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

传统方式:

 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. sql 首写字母查询姓名(字段)

    来自网上大神,不知道是谁,挂不上链接 /////////////////////// 1.生成方法函数 create function f_GetPy(@str nvarchar(4000)) ret ...

  2. jmeter(七)定时器

    知识来源有点复杂,其他测试工作者的博客,百度百科,搜集的电子文档,个人理解等等,限于水平和理解能力,可能有些内容有错误的地方... jmeter提供了很多元件,帮助我们更好的完成各种场景的性能测试,其 ...

  3. .Net JIT

    .Net JIT(转) JIT

  4. 由12306动态验证码想到的ASP.NET实现动态GIF验证码(附源码)

    背景: 12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样.而多家 ...

  5. 从头写个http client(java)

    不熟悉java,但我熟悉http,然后从头打造个简单的httpclient,支持get/post,支持gzip,支持重定向,支持encoding,支持transfer-encoding,支持ssl,支 ...

  6. SOA总结(图片打开略慢请知晓)

  7. Could not execute action

    2.Could not execute action 原因:action成员变量有空值,要访问方法中,使用了该成员变量 参考: http://www.blogjava.net/javagrass/ar ...

  8. hdu3087 LCA + 暴力

    Network Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  9. 【USACO 2.4】The Tamworth Two

    题意:C代表cows,F代表farmer,一开始都向北,每分钟前进1步,如果前方不能走,则这分钟顺时针转90°,问多少步能相遇,或者是否不可能相遇,10*10的地图. 题解:dfs,记录状态,C和F的 ...

  10. 入手了[云梯的VPN]--水文

    之前写的文章 http://www.cnblogs.com/rollenholt/p/3783084.html 结果很多朋友都说访问不了了,现在重新发一下: 各位看官,这是一篇水文: 在用了一段时间s ...