如何打印出的子过程用C#版画的价值? [英] How to print out the value that subprocess prints with C#?

查看:232
本文介绍了如何打印出的子过程用C#版画的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于被要求在此发布,我可以使用Python subprocess.Popen()函数打印出运行Ruby的代码的值。

As is asked in this post, I can use Python's subprocess.Popen() function to print out the value from running ruby's code.

import subprocess
import sys

cmd = ["ruby", "/Users/smcho/Desktop/testit.rb"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
    print line, 
    sys.stdout.flush() 
p.wait()

如何用C#做同样的事情?一个人怎么可以打印哪些子打印出

How can I do the same thing with C#? How can one print the value what the subprocess prints out?

推荐答案

您需要重定向产卵子进程时,标准输出的价值?; MSDN有一个完整的例子: http://msdn.microsoft.com/en-美国/库/ system.diagnostics.processstartinfo.redirectstandardoutput.aspx

You need to redirect stdout when spawning the child process; MSDN has a complete example: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

(从MSDN):

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

这篇关于如何打印出的子过程用C#版画的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆