我在从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#中缺少一些信息。具体地说,没有来自蟒蛇输出的“>>”的行。 有什么办法,我可以在C#中找到那些缺失的行吗?
答案 0 :(得分:1)
我认为(猜测)你也应该设置RedirectStandardError = true
;似乎这些附加信息传递给标准错误。您可以使用ErrorDataReceived
来处理这些行。