0

我在从 C# 中的进程 StandardOutput 读取数据时遇到问题。这是我所拥有的:

var hdd = new System.Diagnostics.Process();
hdd.StartInfo.FileName = "C:\\Program Files\\Java\\jre7\\bin\\java.exe";
hdd.StartInfo.Arguments = "-jar minecraft.jar";
hdd.StartInfo.RedirectStandardOutput = true;
hdd.StartInfo.UseShellExecute = false;
hdd.Start();
while (!hdd.StandardOutput.EndOfStream)
{
    string data = hdd.StandardOutput.ReadLine();
    Console.WriteLine(">> " + data);
}

在这里,我有第二个代码,用 Python 编写:

cmd = '"C:\\Program Files\\Java\\jre7\\bin\\java.exe" -jar minecraft.jar'
import subprocess
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
while True:
    line = p.stdout.readline()
    print '>> ', line.strip()
    if line == '' and p.poll() != None:
        break

问题是,这两个代码都在工作,但在 python 中,脚本打印的信息是 C# 的两倍。

C# 输出:

>> 229 recipes
>> 27 achievements
>> 
>> Starting up SoundSystem...
>> Initializing LWJGL OpenAL
>>     (The LWJGL binding of OpenAL.  For more information, see http://www.lwjgl.org)
>> OpenAL initialized.

Python的输出:

>>  229 recipes
>>  27 achievements
2013-05-07 18:57:12 [CLIENT] [INFO] LWJGL Version: 2.4.2
>>
>>  Starting up SoundSystem...
>>  Initializing LWJGL OpenAL
>>  (The LWJGL binding of OpenAL.  For more information, see http://www.lwjgl.or
g)
>>  OpenAL initialized.
>>
2013-05-07 18:57:14 [CLIENT] [INFO] Found animation info for: textures/blocks/lava_flow.txt
2013-05-07 18:57:14 [CLIENT] [INFO] Found animation info for: textures/blocks/water_flow.txt
2013-05-07 18:57:14 [CLIENT] [INFO] Found animation info for: textures/blocks/fire_0.txt
7 more lines similar to last 3 above

所以我们可以清楚地看到,C#中缺少一些信息。具体来说,pythons输出中没有“>>”的行。有什么办法,我可以在 C# 中捕捉那些缺失的行吗?

4

1 回答 1

1

我认为(猜测)你也应该设置RedirectStandardError = true;似乎这些附加信息正在传递给标准错误。您可以ErrorDataReceived用于处理这些行。

于 2013-05-07T17:50:51.720 回答