C#调用adb的命令
(2012-10-24 15:31:22)
标签:
adbcsharpcit |
分类: 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的工作原理,基本一样的代码,为什么自己写的还是得不到其输出内容?
确实和其他命令行程序不太一样。。。
前一篇:jpeg图片Thumbnail