【问题标题】:OutputDataReceived is not equal to the cmd output. Why?OutputDataReceived 不等于 cmd 输出。为什么?
【发布时间】:2017-03-21 16:19:01
【问题描述】:

我是这样创建流程的:

Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd",
                Arguments = "/C tools\\adb " + serial + command,
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
            }
        };
        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();

然后我订阅事件:

process.OutputDataReceived += Display;

我将数据附加到 StringBuilder:

builder.AppendLine(e.Data);

最后,我将 StringBuilder 文本附加到 RichTextBox。

rtb_console.AppendText(builder.ToString());

问题是,我得到的输出不等于我用 cmd 得到的输出。

例如

CMD(正确):

Line 1 text text text text
Line 2 text text text text
Line 3 text text text text

使用 C#(错误):

Line 1 text text text text

Line 2 text text text text

Line 3 text text text text

当我执行时,我也得到了错误的输出

adb logcat -d > output.txt

如果我使用 StandardOutput.ReadToEnd() 不会出现问题,但是我不会得到实时输出。

我找不到问题。你能帮帮我吗?

真诚的

【问题讨论】:

  • 好吧,“stuff > file”会阻止标准输出,所以不,你不会看到实时输出,它会在 output.txt 中。第二,它似乎在捕获换行符,然后你重新编写它们..(你还没有展示你是如何做到的)所以..不要使用 writeline..
  • 我不在程序中使用“stuff > file”。我订阅了该活动。事件触发 -> 数据写入 StringBuilder -> 将数据附加到 RichTextBox
  • 您在 OutputDataReceived 事件处理程序中收到的文本已经包含行终止符。 AppendLine() 添加 另一个 一个。请改用 Append()。
  • 感谢两位的帮助。找到了答案。请参阅下面的帖子。

标签: c# cmd redirectstandardoutput


【解决方案1】:

嗯,终于找到答案了。

关键是,使用不带 pty 的 shell 命令。 Pty 破坏二进制输出。

代替

adb shell ls

使用

adb exec-out ls

更多信息:

Read binary stdout data from adb shell?

https://android.googlesource.com/platform/system/core/+/5d9d434efadf1c535c7fea634d5306e18c68ef1f

Transferring binary data over ADB shell (ie fast file transfer using tar)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多