库
using System.Diagnostics;
private Process cmd_process;
下发单条adb指令
代码示例
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;
}
需要注意
1. cmd_process.StartInfo.FileName = adb_exe_path;
public string adb_exe_path { get; } = "C:/Users/nick/adb-fastboot/adb.exe";
博主使用的adb.exe是绝对路径。
2. string arguments = GetAdbCommandsArguments(cmds);
cmd_process.StartInfo.Arguments = arguments;
这里的argument 为不包含adb 这个字符的argument,比如下发 “adb shell” ,“shell” 为这个程序的argument,因此只要设置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();
在通过读取adb 命令返回的过程中,有可能会出现卡死现象,在操作完 reader.ReadToEnd(),后将reader 关闭,执行reader.Close(); 可以优化这个问题。
同时下发多条adb指令-通过将指令存储在txt文件中,读取文件方式实现
代码示例
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;
}
需要注意
1. process.StartInfo.FileName = adb_exe_path;
博主使用的adb.exe是绝对路径。同上
2. Arguments 设置为shell,文件中写入在实际情况中 adb shell后 直接操作dut的指令
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(); |
文件内容示例
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. 多条指令可能会遇到内存溢出问题
// // 摘要: // 读取来自流的当前位置到结尾的所有字符。 // // 返回结果: // 字符串形式的流的其余部分(从当前位置到结尾)。 如果当前位置位于流结尾,则返回空字符串 (“”)。 // // 异常: // T:System.OutOfMemoryException: // 没有足够的内存来为返回的字符串分配缓冲区。 // // T:System.IO.IOException: // 出现 I/O 错误。 public override string ReadToEnd(); |
通过指令定义,也可以看到,如果执行多条指令后,如果指令返回值很大的情况下,有可能会有没有足够的内存来为返回的字符串分配缓冲区。因此在执行程序出现崩溃的情况时可以考虑是否指令返回信息过大,因此可能通过将指令分流下发来解决该问题。