Search code examples
c#androidshellexceptionadb

How to get Exception adb Error Code in C#


Ηow to get error exception for adb?

Like if no adb devices connected richtextbox give no devices connected and don't write any thing in richtextbox, or if device connected richtextbox write reading data ok then show all information about commands

this is my code :

// START READ INFO//
//MANFACTURE//
using (Process process = new Process())
{
    ProcessStartInfo startInfo = new ProcessStartInfo
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        FileName = "adb.exe",
        Arguments = " shell getprop ro.product.manufacturer"
    };
    process.StartInfo = startInfo;
    process.Start();
    process.StartInfo.RedirectStandardError = true;
    Sks.Text = process.StandardOutput.ReadToEnd();
    string b = "MANUFACTURE :" + Sks.Text;
    progressBar1.Value = 0;
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 15;
    progressBar1.Step = 1;

    for (int i = 0; i < 100; i++)
    {
        progressBar1.PerformStep();
    }
   // SECURITY PATCH //
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.CreateNoWindow = true;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.FileName = "adb.exe";
    startInfo.Arguments = "shell getprop ro.build.version.security_patch";
    process.StartInfo = startInfo;
    process.Start();
    Sks.Text = process.StandardOutput.ReadToEnd();
    string E = Environment.NewLine + "SECURITY PATCH :" + Sks.Text;
    string s = Sks.Text = b + E;

Solution

  • process.StartInfo = startInfo;
    process.Start();
    process.StartInfo.RedirectStandardError = true;
    Sks.Text = process.StandardOutput.ReadToEnd();
    

    There are 2 problems here:

    1. You need to redirect before you start the process
    2. The error is in StandardError

    Here is how you should do it:

    process.StartInfo = startInfo;
    process.StartInfo.RedirectStandardError = true;
    process.Start();
    
    Sks.Text = process.StandardOutput.ReadToEnd();
    var error = process.StandardError.ReadToEnd(); // "error: no devices/emulators found"
    if (!string.IsNullOrEmpty(error))
    {
        // do something...
    }