[c#][process] Issue single and multiple adb commands at the same time

Library

using System. Diagnostics;

private Process cmd_process;

Issue a single adb command

Code sample

 private string GetAdbCommandsArguments(string cmds)
        {
            cmds = cmds. Replace("adb", "");
            cmds = cmds. Trim();
            return cmds;
        }

        public string RunAdbCommands(string cmds)
        {
           
            string returnValue;
            string arguments = GetAdbCommandsArguments(cmds);
            cmd_process = new Process();
            cmd_process.StartInfo.FileName = adb_exe_path;
            cmd_process.StartInfo.UseShellExecute = false;
            cmd_process.StartInfo.CreateNoWindow = true;
            cmd_process.StartInfo.RedirectStandardOutput = true;
            cmd_process.StartInfo.RedirectStandardInput = true;
            cmd_process.StartInfo.RedirectStandardError = true;
            cmd_process.StartInfo.Arguments = arguments;
            try
            {
               
                      
                cmd_process. Start();
                StreamReader reader = cmd_process. StandardOutput;
                returnValue = reader. ReadToEnd();
                reader. Close();
                cmd_process.WaitForExit();
                cmd_process.Close();
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
            return returnValue;
        }

Attention required

1. cmd_process.StartInfo.FileName = adb_exe_path;

public string adb_exe_path { get; } = “C:/Users/nick/adb-fastboot/adb.exe”;

The adb.exe used by the blogger is an absolute path.

2. string arguments = GetAdbCommandsArguments(cmds);

cmd_process.StartInfo.Arguments = arguments;

The argument here is an argument that does not contain the character adb, such as sending “adb shell”, “shell” is the argument of this program, so just set cmd_process.StartInfo.Arguments = shell;

3. cmd_process. Start();
StreamReader reader = cmd_process. StandardOutput;
returnValue = reader. ReadToEnd();
reader. Close();
cmd_process.WaitForExit();
cmd_process.Close();

In the process of returning by reading the adb command, there may be a stuck phenomenon. After operating reader.ReadToEnd(), close the reader and execute reader.Close(); this problem can be optimized.

Issue multiple adb commands at the same time – by storing the commands in a txt file and reading the file

Code sample

 public string RunAdbCommandsFromFiles(string file_name)
        {
            string outPut="";
            Process process = new Process();
            process.StartInfo.FileName = adb_exe_path;
            process.StartInfo.Arguments = "shell";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            
            process. Start();
            FileStream fs = new FileStream(file_name, FileMode. Open, FileAccess. Read);
            StreamReader read = new StreamReader(fs, Encoding. Default);
            string strReadline;
            
            while ((strReadline = read. ReadLine()) != null)
            {
               process.StandardInput.WriteLine(strReadline);
                   
            }
         
            fs.Close();
            read. Close();
            process.StandardInput.WriteLine("exit");
            process.StandardInput.AutoFlush=true;
            process.StandardInput.Close();
                
            try
            {
                StreamReader output_reader = process. StandardOutput;
                outPut = output_reader. ReadToEnd();
                output_reader. Close();
            }
             catch (Exception e)
            {
                        return e.Message;
            }
            
         process.WaitForExit();
         process. Close();
         return outPut;
        }

Attention required

1. process.StartInfo.FileName = adb_exe_path;

The adb.exe used by the blogger is an absolute path. ditto

2. Set Arguments to shell, and write in the file the command to directly operate dut after adb shell in the actual situation

process.StartInfo.Arguments = “shell”;

FileStream fs = new FileStream(file_name, FileMode. Open, FileAccess. Read);
StreamReader read = new StreamReader(fs, Encoding. Default);
string strReadline;
while ((strReadline = read. ReadLine()) != null)
{
process.StandardInput.WriteLine(strReadline);
}

fs.Close();
read. Close();
process.StandardInput.WriteLine(“exit”);
process.StandardInput.AutoFlush=true;
process.StandardInput.Close();

Example of file content

ls -l /sys/kernel /debug/XXX1
ls -l /sys/kernel/debug/XXX2
ls -l /sys/kernel/debug/XXX3
ls -l /sys/kernel/debug/XXX4
ls -l /sys/kernel/debug/XXX5
ls -l /sys/kernel/debug/XXX6

3. Multiple instructions may encounter memory overflow problems

//
// Summary:
// Read all characters from the current position of the stream to the end.
//
// return result:
// The remainder of the stream (from the current position to the end) as a string. Returns an empty string (“”) if the current position is at the end of the stream.
//
// exception:
// T: System. OutOfMemoryException:
// There is not enough memory to allocate a buffer for the returned string.
//
// T: System.IO.IOException:
// An I/O error occurred.
public override string ReadToEnd();

Through the instruction definition, it can also be seen that if multiple instructions are executed, if the return value of the instruction is large, there may not be enough memory to allocate a buffer for the returned string. Therefore, when the execution program crashes, you can consider whether the instruction return information is too large, so it is possible to solve the problem by shunting the instructions.