OutputDataReceived不等于cmd输出.为什么?

OutputDataReceived is not equal to the cmd output. Why?

I create the process this way:

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();

Then I subscribe to the event:

process.OutputDataReceived += Display;

I append the data to the StringBuilder:

builder.AppendLine(e.Data);

Finally i append the StringBuilder text to a RichTextBox.

rtb_console.AppendText(builder.ToString());

The problem is, that the the output I get is not equal to the output i get with cmd.

E.g.

CMD (correct):

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

With C# (wrong):

Line 1 text text text text

Line 2 text text text text

Line 3 text text text text

I also get the wrong output, when I execute

adb logcat -d > output.txt

The problem does not occur, if I use StandardOutput.ReadToEnd(), but then I will not get the live output.

I can not find the problem. Can you help me?

Sincerely

Well, finally I found the answer.

The key is, to use shell commands with no pty. Pty mangles the binary output.

Instead of

adb shell ls

use

adb exec-out ls

More information:

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)