1: Synchronous example

  1. static void runCommand() {
  2. Process process = new Process();
  3. process.StartInfo.FileName = "cmd.exe";
  4. process.StartInfo.Arguments = "/c DIR"; // Note the /c command (*)
  5. process.StartInfo.UseShellExecute = false;
  6. process.StartInfo.RedirectStandardOutput = true;
  7. process.StartInfo.RedirectStandardError = true;
  8. process.Start();
  9. //* Read the output (or the error)
  10. string output = process.StandardOutput.ReadToEnd();
  11. Console.WriteLine(output);
  12. string err = process.StandardError.ReadToEnd();
  13. Console.WriteLine(err);
  14. process.WaitForExit();
  15. }

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

  1. static void runCommand() {
  2. //* Create your Process
  3. Process process = new Process();
  4. process.StartInfo.FileName = "cmd.exe";
  5. process.StartInfo.Arguments = "/c DIR";
  6. process.StartInfo.UseShellExecute = false;
  7. process.StartInfo.RedirectStandardOutput = true;
  8. process.StartInfo.RedirectStandardError = true;
  9. //* Set your output and error (asynchronous) handlers
  10. process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
  11. process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
  12. //* Start process and handlers
  13. process.Start();
  14. process.BeginOutputReadLine();
  15. process.BeginErrorReadLine();
  16. process.WaitForExit();
  17. }
  18. static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
  19. //* Do your stuff with the output (write to console/log/StringBuilder)
  20. Console.WriteLine(outLine.Data);
  21. }

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

  1. //* Set your output and error (asynchronous) handlers
  2. process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
  3. 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. egg

    简介 阿里的   红色的是项目名字 egg-init --type simple First && cd First  egg-init --type simple Second &a ...

  2. 溶解shader

    玩神界原罪2,感觉人物被建筑遮挡时,建筑的“溶解”效果很有意思,想实现一下.然后发现连溶解都没实现过,emmmmm....先来把溶解实现了~ 原理就是根据一张噪声图的值是否大于某个阈值,来判断是否丢弃 ...

  3. (转)heartbeat原理及部署

    原文:http://yjy724.blog.51cto.com/10897133/1840794---------------------------------------------------h ...

  4. MySQL比较运算符的子查询

    使用比较运算符的子查询 =.>.<.>=.<=.<>.!=.<=> 语法结构 operand comparison_operator subquery ...

  5. Java线程问题(基础回顾)

    1.概念:线程是运行程序(进程)中单个顺序的小程序,一个进程可以由多个线程组成,而这多个线程共享同一个存储空间,这使得线程间的通信比较容易.在一个多进程的程序中,如果要切换到另一个进程,需要改变地址空 ...

  6. c++ 网络编程(七) LINUX下 socket编程 基于套接字的标准I/O函数使用 与 fopen,feof,fgets,fputs函数用法

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614820.html 一.标准I/O 1,什么是标准I/O?其实是指C语言里的文件操作函数,如 ...

  7. light table 添加行号 更新

    在上一个笔记修改完字体后.再添加上行号

  8. Unity Github 项目收集

    http://gad.qq.com/article/detail/24048 重磅推荐: Github 热门 Unity Assets 查询:http://unitylist.com/browse 最 ...

  9. Python基础(3) - 数据类型:1数字类型

    Python数据类型 数据类型 是否容器 是否可变 存储方式 数字 否 否 直接 字符串 否 否 直接 列表 是 是 顺序 元组 是 否 顺序 字典 是 是 映射 数字类型 整  型:1,234,0, ...

  10. javascript进行base64加密,解密

    function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...