如何用C#打印子进程打印的值?

发布于 2024-10-17 03:00:51 字数 480 浏览 2 评论 0原文

正如这篇帖子中所要求的那样,我可以使用 Python 的 subprocess.Popen() 函数打印出运行 ruby​​ 代码的值。

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# 做同样的事情?如何打印子进程打印出来的值?

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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你丑哭了我 2024-10-24 03:00:51

生成子进程时需要重定向 stdout; MSDN 有一个完整的示例: 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();

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

(from 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();
请爱~陌生人 2024-10-24 03:00:51
ProcessStartInfo psi = new ProcessStartInfo("ruby", "/Users/smcho/Desktop/testit.rb");
psi.RedirectStandardOuput = true;W    
Process proc = new Process(psi);
proc.Start();
StreamReader stdout = proc.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null)
   Console.WriteLine(line);
ProcessStartInfo psi = new ProcessStartInfo("ruby", "/Users/smcho/Desktop/testit.rb");
psi.RedirectStandardOuput = true;W    
Process proc = new Process(psi);
proc.Start();
StreamReader stdout = proc.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null)
   Console.WriteLine(line);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文