加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#调用adb的命令

(2012-10-24 15:31:22)
标签:

adb

csharp

c

it

分类: Android
想从C#程序调用adb pull,把手机中的文件拷贝到电脑上来,
不好弄啊不好弄,这个问题纠结了一天,最后还是找到了个开源的代码搞出来了,
DroidExplorer!
特意加粗加红表达对该小组高手的敬意。
下面看程序吧
///
/// Runs the adb command.
///
/// The device.
/// The command.
/// The args.
///
private string RunAdbCommand ( string device, AdbCommand command, string args, bool wait ) {
try {
StringBuilder result = new StringBuilder ( );
Process proc = new Process ( );
StringBuilder commandArg = new StringBuilder ( AdbCommandArguments ( device, command ) );
if ( !string.IsNullOrEmpty ( args ) ) {
commandArg.AppendFormat ( " {0}", args );
}
ProcessStartInfo psi = new ProcessStartInfo ( GetSdkTool ( ADB_COMMAND ), commandArg.ToString ( ) );
this.LogDebug ( "{0} {1}", System.IO.Path.GetFileName ( psi.FileName ), psi.Arguments );

psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo = psi;
proc.OutputDataReceived += delegate ( object sender, DataReceivedEventArgs e ) {
if ( !string.IsNullOrEmpty ( e.Data ) ) {
result.AppendLine ( e.Data.Trim ( ) );
}
};

proc.ErrorDataReceived += delegate ( object sender, DataReceivedEventArgs e ) {
if ( !string.IsNullOrEmpty ( e.Data ) ) {
result.AppendLine ( e.Data.Trim ( ) );
}
};

proc.Exited += delegate ( object sender, EventArgs e ) {

};
proc.Start ( );
proc.BeginOutputReadLine ( );
proc.BeginErrorReadLine ( );

if ( wait ) {
proc.WaitForExit ( );
} else {
Thread.Sleep ( 250 );
}

return result.ToString ( );
} catch ( Win32Exception wex ) {
this.LogError ( wex.Message, wex );
} catch ( Exception ex ) {
this.LogError ( ex.Message, ex );
}
return string.Empty;
}

还是不太明白adb的工作原理,基本一样的代码,为什么自己写的还是得不到其输出内容?
确实和其他命令行程序不太一样。。。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有