Search code examples
c#powershelladb

how to fetch all files from adb with powershell and C#


Hi I have automation code in c# that I need to check if the database files are encrypted, but first I need to download the files into my machine.

    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);
    }

first how do I know to where the files are downloaded ? second how to save to specific folder ?


Solution

  • You have two options to do that:

    Use plain Process (recommended)

    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}");
    

    Use PowerShell (your case)

    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();
    }