.Net 通过Cmd执行Adb命令 /c参数
通过cmd.exe来执行adb命令,可以进行一些命令组合,直接用adb.exe的话只能执行单个adb命令
这里要注意cmd 中的/c参数,指明此参数时,他将执行整个字符串中包含的命令并退出当前cmd运行环境
如:
命令:C:\Windows\system32>cmd /c E:\Work\Projects\lenovo_lmsa\lmsa-client\Bin\adb.exe devices | findstr "3"
3b2ac4c5 device
命令执行成功
当去掉/c时,将一直处于执行状态并不会退出(当通过下面的方法执行时WaitForExit方法将处于阻塞状态)
命令:C:\Windows\system32>cmd E:\Work\Projects\lenovo_lmsa\lmsa-client\Bin\adb.exe devices | findstr "3"
当去掉/c 与管道命令时,执行情况如下(同命令C:\Windows\system32>cmd结果一致)
命令:C:\Windows\system32>cmd E:\Work\Projects\lenovo_lmsa\lmsa-client\Bin\adb.exe devices
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
所以,在代码中一定得注意/c参数的使用
我在代码中执行 adb install -s serial -r file.apk时,如果目标设备未连接,此adb命令会阻塞等待设备连接,此时线程会阻塞,所以我执行adb devices | findstr "serial" && adb adb install -s serial -r file.apk命令,先判断目标设备是否还连接,然后在装apk,能大大降低阻塞的概率
方法调用代码片段(当命令较复杂时,最好用括号括起来,比如有路径什么的时候,不用括号把命令括起来,命令解析会失败)
string exe = System.IO.Path.Combine(Environment.SystemDirectory, "cmd.exe");
string targetCmd = string.Format("/c (\"{0}\" devices | findstr \"{1}\" && \"{0}\" -s \"{1}\" {2})"
, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"adb.exe")
, deviceId
,sourceCommand);
Process方法代码:
public string Do(string exe, string command, int timeout)
{
List<string> response = new List<string>();
List<string> output = new List<string>();
List<string> error = new List<string>();
Process process = new Process(); process.StartInfo.FileName = exe;
process.StartInfo.Arguments = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.EnableRaisingEvents = true;
System.IO.FileInfo file = new System.IO.FileInfo(exe);
process.StartInfo.WorkingDirectory = file.Directory.FullName;
process.OutputDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) => Redirected(output, sender, e);
process.ErrorDataReceived += (object sender, System.Diagnostics.DataReceivedEventArgs e) => Redirected(error, sender, e);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
bool exited = process.WaitForExit(timeout);
if (!exited)
{
process.Kill();
response.Add("Error: timed out");
}
response.AddRange(output);
response.AddRange(error);
string data = String.Join("\r\n", response.ToArray());
if (data.EndsWith("\r\n"))
{
data = data.Remove(data.LastIndexOf("\r\n"));
}
return data;
}
.Net 通过Cmd执行Adb命令 /c参数的更多相关文章
- Kubernetes(k8s)为容器设置启动时要执行的命令和参数
创建 Pod 时设置命令及参数 创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数.如果要设置命令,就填写在配置文件的 command 字段下,如果要设置命令的参数,就填写在配置文件的 ...
- CMD执行BCP命令
C:\>BCP "EXEC GetU '2016-7-11' ,'-1'" queryout "C:\\C3Marketing\SummaryReport_test ...
- windows在cmd执行svn 命令
1. 使用svn 命令行工具. 找到http://www.visualsvn.com/downloads/ 下载Apache Subversion command line tools,这是一个可以在 ...
- 通过ant调用shell脚本执行adb命令
在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...
- java调用cmd执行maven命令
一.原理介绍 Java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后封闭命令窗口. cmd /k di ...
- Python脚本实现在cmd执行相关命令
通过Python脚本实现,在cmd命令执行文件的cp(复制).rm(删除).rename(重命名).move(文件移动).mkdir(创建目录) cmd执行命令格式:python xxx.py 用户名 ...
- 在go中通过cmd调用python命令行参数量级过大问题解决
问题描述如下: 在go中使用cmd调用python命令行 cmd := exec.Command("python", "dimine/Kriging/matrix.py& ...
- mac eclipse 执行adb 命令,提示command not fount
java执行命令: p = Runtime.getRuntime().exec(commandStr); [INFO ] 执行命令结果:nullbash: adb: command not found ...
- cmd执行jmeter命令生成报告的问题。
现有几个jmeter脚本,准备以命令行的方式执行jmeter脚本,并生成报告. 一.使用python语言处理 1.目录结构 2.说明 jmx目录下是jmeter脚本 result目录下是生成的报告及文 ...
随机推荐
- [Javascript] Write a function pipeline
const _pipe = (f, g) => (...args) => g(f(...args)) export const pipe = (...fns) => fns.redu ...
- ppt转flash kindeditor上传视频全屏问题
最近要增加页面的ppt显示功能,于是考虑把ppt转成flash,在网上搜到了ispingfree,链接: https://pan.baidu.com/s/1QZzx6qmdsnwzWCuULXzUOw ...
- Java一些基本帮助类
Scanner sc=new Scanner(System.in); sc.nextInt(); sc.next(); Random ran=new Random(); ran.nextInt(); ...
- FreeBSD 5.0中强制访问控制机制的使用与源代码分析【转】
本文主要讲述FreeBSD 5.0操作系统中新增的重要安全机制,即强制访问控制机制(MAC)的使用与源代码分析,主要包括强制访问控制框架及多级安全(MLS)策略两部分内容.这一部分讲述要将MAC框架与 ...
- Linux核心设计依据(六)该块I/O一层
块设备是能随机存取装置固定大小的数据表设备.如硬盘:字符设备(如串口和键盘)它是按照字符流进入有序进行.不同之处在于是否足够的随机存取数据--这时候,你可以随心所欲地从一个位置跳到访问设备和位置.复杂 ...
- 让delphi2010操作界面回到delphi7模式
让delphi2010操作界面回到delphi7模式 在使用delphi2010的过程中,很不习惯它的窗口在一个框框内,感觉很不方便,可能是因为使用delphi7很多年了,已经习惯了delphi7的版 ...
- WPF 中那些可跨线程访问的 DispatcherObject(WPF Free Threaded Dispatcher Object)
原文 WPF 中那些可跨线程访问的 DispatcherObject(WPF Free Threaded Dispatcher Object) 众所周知的,WPF 中多数对象都继承自 Dispatch ...
- Win7使用初体验
作者:朱金灿 来源:http://blog.csdn.net/clever101 最近装了个Win7旗舰版.随着年纪渐大,我对软件使用越来越趋于务实,不再追求最新版本,因为我评价一个好软件的标准是适用 ...
- VAssist 使用技巧(函数声明定位,比VS的还要强大)
1. 有了VAX可以关掉C++导航栏,快捷键ALT+M,显示当前打开文档的所有符号,而且可以输入进行过滤 2. 查找文件,shift+alt+o (直接定位) 3. 查找符号shift+alt+s 4 ...
- HDU 1010 Tempter of the Bone heuristic 修剪
的问题是,在测试修剪. 应该说是更先进的应用. 由于使用的heuristic(经验)修剪.总结这方面的经验法则,别easy.我说,这也是由于先进的在线报告中的应用程序没有分析太多太好的解决这个问题,计 ...