-2

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

Labo
  • 99
  • 9
  • 1
    well "stuff > file" blocks standard out, so no you wouldnt see live output, it would be in output.txt .. second, it seems as if its capturing the linefeeds, and then you're writelining them.. (you havent shown how you do that) so.. dont use writeline.. – BugFinder Mar 21 '17 at 16:24
  • I do not use "stuff > file" in the program. I subscribe to the event. Event fires -> data is written to StringBuilder -> append data to RichTextBox – Labo Mar 21 '17 at 16:30
  • 1
    The text you received in your OutputDataReceived event handler already includes the line terminator. AppendLine() adds *another* one. Use Append() instead. – Hans Passant Mar 21 '17 at 17:35
  • Thank you both for help. Found the answer. See the post below. – Labo Mar 21 '17 at 17:56

1 Answers1

-2

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)

Community
  • 1
  • 1
Labo
  • 99
  • 9