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# で同じことを行うにはどうすればよいですか? サブプロセスが出力する値をどのように出力できますか?

4

2 に答える 2

6

子プロセスを生成するときに 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();
于 2011-02-12T10:38:21.707 に答える
2
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);
于 2011-02-12T10:44:10.463 に答える