【问题标题】:Process redirect standard output verbosity issue处理重定向标准输出冗长问题
【发布时间】:2013-05-01 17:55:08
【问题描述】:

我在从 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# 中捕捉那些缺失的行吗?

【问题讨论】:

  • 会不会是python捕获的调试输出?如果您download and run DbgView 并在运行 C# 程序时启动它并让它继续运行,您是否看到 DbgView 窗口中出现类似的内容?
  • @Matthew Watson - 不知何故,我无法获得任何实际输出。它说 [4516] FMAPO:调试功能已禁用(DbgView 以管理员身份运行,无论我手动调试还是运行应用程序都没有关系)

标签: c# python redirect output minecraft


【解决方案1】:

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

【讨论】:

  • Aww,我不敢相信它是如此简单 :( 完成并工作,它确实传递给了 StandardError。谢谢!
  • 我很高兴有帮助; :)
猜你喜欢
  • 1970-01-01
  • 2012-11-04
  • 2014-01-20
  • 2011-12-08
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多