首页 > 解决方案 > 如何使用 powershell 和 C# 从 adb 中获取所有文件

问题描述

嗨,我在 c# 中有自动化代码,我需要检查数据库文件是否已加密,但首先我需要将文件下载到我的机器中。

    try
    {
        string path = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName;

        Directory.SetCurrentDirectory(path + "\\powershell\\");
        using (PowerShell pshell = PowerShell.Create())
        {
            pshell.AddCommand("adb pull \"/data/data/app.name.tt/files/Database\"");
            //pshell.AddScript(path + "\\powershell\\AdbPull.Ps1");
            pshell.Invoke();
        }
    }
    catch (DirectoryNotFoundException e)
    {
        Console.WriteLine("The specified directory does not exist. {0}", e);
    }

首先我怎么知道文件下载到哪里?第二如何保存到特定文件夹?

标签: c#powershelladb

解决方案


您有两种选择:

使用普通流程(推荐)

var adbPath = "C:/Program Files/ADB/adb.exe";
var command = "pull";
var whatToPull = "\"/storage/77F1-1BF3/sht/1.jpg\"";
var whereToPull = "\"C:/Users/picolino/adb/downloads\"";

Process.Start(adbPath, $"{command} {whatToPull} {whereToPull}");

使用 PowerShell(你的情况)

var adbPath = "C:/Program Files/ADB/adb.exe";
var command = "pull";
var whatToPull = "\"/storage/77F1-1BF3/sht/1.jpg\"";
var whereToPull = "\"C:/Users/picolino/adb/downloads\"";

using (var pShell = PowerShell.Create())
{
    pShell.AddScript($"& '{adbPath}' {command} {whatToPull} {whereToPull}");
    pShell.Invoke();
}

推荐阅读