1: Synchronous example

static void runCommand() {
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c DIR"; // Note the /c command (*)
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = process.StandardError.ReadToEnd();
Console.WriteLine(err);
process.WaitForExit();
}

Note that it's better to process both output and errors: they must be handled separately.

(*) For some commands (here StartInfo.Arguments) you must add the /c directive, otherwise the process freezes in the WaitForExit().

2: Asynchronous example

static void runCommand() {
//* Create your Process
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c DIR";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//* Set your output and error (asynchronous) handlers
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
//* Start process and handlers
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
//* Do your stuff with the output (write to console/log/StringBuilder)
Console.WriteLine(outLine.Data);
}

If you don't need to do complicate operations with the output, you can bypass the OutputHandler method, just adding the handlers directly inline:

//* Set your output and error (asynchronous) handlers
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
process.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);

Process.start: how to get the output?的更多相关文章

  1. Scoring and Modeling—— Underwriting and Loan Approval Process

    https://www.fdic.gov/regulations/examinations/credit_card/ch8.html Types of Scoring FICO Scores    V ...

  2. Child Process模块

    目录 exec() execSync() execFile() spawn() fork() send() 参考链接 child_process模块用于新建子进程.子进程的运行结果储存在系统缓存之中( ...

  3. [转载]Process工具类,提供设置timeout功能

    FROM:http://segmentfault.com/blog/lidonghao/1190000000372535 在前一篇博文中,简单介绍了如何使用Process类来调用命令行的功能,那样使用 ...

  4. Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

    https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  5. Codeforces 946 B.Weird Subtraction Process

    B. Weird Subtraction Process   time limit per test 1 second memory limit per test 256 megabytes inpu ...

  6. nodejs-Child Process模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Child Process模块 GitHub TOP Child Process模块 来自<JavaScript 标准参 ...

  7. MySQL、MongoDB、Redis数据库Docker镜像制作

    MySQL.MongoDB.Redis数据库Docker镜像制作 在多台主机上进行数据库部署时,如果使用传统的MySQL的交互式的安装方式将会重复很多遍.如果做成镜像,那么我们只需要make once ...

  8. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  9. 执行non-Java processes命令行的工具ExecHelper

    在Java程序里执行操作命令行工具类: public static void main(String[] args) { try { /*String file = ExecHelper.exec( ...

随机推荐

  1. Bellman-Ford(BF)和Floyd算法

    以下只是本人的笔记,想法我自己都怀疑,内容不作为参考, Floyd算法就比较暴力了,算法思想是三重循环,直接枚举所有的顶点,再两次for循环枚举所有点,验证以第一个点为中转点的两个点是否路径更短,具体 ...

  2. docker安装mongo初体验

    1.docker安装在此不做介绍,请自行百度:个人环境是在windows下运行cmd执行docker命令2.打算在docker下安装使用mongo,顺带熟悉docker,拉开篇章3.docker下安装 ...

  3. Oracle 中运用rollup和cube实现汇总运算

    前言.看了很多的随笔博文内容都是关于rollup和cube的用法,发现一个问题,很多都是一样或者转载的,但这都不是重点,重点是,他们写的都太专业化了,直接给一个结论,并没有给出整个推理出这个结论的过程 ...

  4. Jenkins~powershell+cmd发布nuget包包

    nuget包也要自动化部署了,想想确实挺好,在实施过程中我们要解决的问题有版本自动控制,nuget自动打包,nuget自动上传到服务端等. 一 参数化构建 二 环境变量的k/v参数,存储类库的初始版本 ...

  5. 转 功能强大的shell:if条件语句

    原文 http://blog.zol.com.cn/2322/article_2321767.html 功能强大的shell:if条件语句 if语句测试条件,测试条件返回真(0)或假(1)后,可相应执 ...

  6. 改修jquery支持cmd规范的seajs

    手动包装jquery1.10.2,firebug说$没有定义 define(function (require, exports, module) {//jquery源码module.exports= ...

  7. pycurl安装问题

    pycurl安装问题 之前人写的代码中依赖pycurl,所以准备在ubuntu14.04.4 LTS系统上安装一下.发现了不少问题. Could not run curl-config 最开始遇到问题 ...

  8. 关于80286——《x86汇编语言:从实模式到保护模式》读书笔记15

    一.80286的工作模式 80286首次提出了实模式和保护模式的概念. 实模式:和8086的工作方式相同: 保护模式:提供了存储器管理机制和保护机制,支持多任务. 二.80286的寄存器 (一)通用寄 ...

  9. 【Linux】Linux C socket 编程之UDP

    发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...

  10. wtl学习总结

    在windows平台,相比MFC,我更喜欢WTL,因其简洁漂亮.所以陆续花了一年的时间学习之,这里总结一下(在学习Wtl/Atl之前,最好是对WinApi编程有一定的了解). 安装 Wtl主页 htt ...