GithubHelp home page GithubHelp logo

jiaweirobot / adbdotnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vurdalakov/adbdotnet

0.0 0.0 0.0 17 KB

A simple .NET C# library to access ADB (Android Debug Bridge) functionality without running `adb.exe`.

License: MIT License

C# 100.00%

adbdotnet's Introduction

AdbDotNet

AdbDotNet is a simple .NET C# library to access ADB (Android Debug Bridge) functionality without running adb.exe.

AdbDotNet communicates directly with ADB server through port 5037 (can be overridden, see below).

This library is distributed under the MIT license.

Usage examples

Minimal application: Get ADB server version

namespace AdbDotNetTestApplication
{
    using System;
    using Vurdalakov;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var adbClient = new AdbClient();
                var version = adbClient.GetServerVersion();
                Console.WriteLine($"ADB server version {version} running on {adbClient.ServerHost} port {adbClient.ServerPort}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[Exception] {ex.Message}");
            }
        }
    }
}

Connect to ADB server running on a different port

Default behavior: if ANDROID_ADB_SERVER_PORT environment variable is set, its value is used as a port number; otherwise port 5037 is used.

var adbClient = new AdbClient(777);

Start ADB server if it is not running

adb.exe start-server from Android SDK directory will be started.

To find Android SDK, the Path value of the following Registry entries is used:

  • HKEY_LOCAL_MACHINE\Software\Wow6432Node\Android SDK Tools for 64-bit Windows;
  • HKEY_LOCAL_MACHINE\Software\Android SDK Tools for 32-bit Windows.
var adbClient = new AdbClient();

try
{
    adbClient.GetServerVersion();
}
catch
{
    Console.WriteLine("ADB server is not running, trying to start it");
    adbClient.StartServer();
}

Get list of connected devices

var devices = adbClient.GetDevices();

Console.WriteLine($"{devices.Length} connected Android devices:");

foreach (var device in devices)
{
    Console.WriteLine($"serialNumber:{device.SerialNumber}, product:{device.Product}, model:{device.Model}, device:{device.Device}");
}

Select device

adbClient.SetDevice(devices[0].SerialNumber);

Console.WriteLine($"Connected to device {devices[0].SerialNumber}");

Get all device properties

var props = adbClient.GetDeviceProperties();

Console.WriteLine($"{props.Count} properties:");

foreach (var prop in props)
{
    Console.WriteLine($"{prop.Key}={prop.Value}");
}

Get specific device properties

var props = adbClient.GetDeviceProperties();

Console.WriteLine($"Manufacturer: {props["ro.product.manufacturer"]}");
Console.WriteLine($"Model: {props["ro.product.model"]}");
Console.WriteLine($"Android version: {props["ro.build.version.release"]}");
Console.WriteLine($"Android SDK: {props["ro.build.version.sdk"]}");

Execute any shell command on device

var response = adbClient.ExecuteRemoteCommand("ls -l /mnt/sdcard/DCIM/Camera");
Console.WriteLine($"Result:\n{String.Join("\r\n", response)}");

Get device directory listing

var fileInfos = adbClient.GetDirectoryListing("/mnt/sdcard/DCIM/Camera");

Console.WriteLine($"{fileInfos.Length} files:");
foreach (var fileInfo in fileInfos)
{
    Console.WriteLine($"{fileInfo.Name} {fileInfo.Size} {fileInfo.Mode} {fileInfo.FullName}");
}

Get device file information

var fileInfo = adbClient.GetFileInfo("/mnt/sdcard/DCIM/Camera/20161130_1732.jpg");

Console.WriteLine($"{fileInfo.Name} {fileInfo.Size} {fileInfo.Mode} {fileInfo.FullName}");

Download file from device

var fileName = adbClient.GetFileInfo("/mnt/sdcard/DCIM/Camera/20161130_1732.jpg");

adbClient.DownloadFile(fileInfo.FullName, @"C:\Temp\" + fileInfo.Name);

Upload file to device

adbClient.UploadFile(@"C:\Temp:\20161130_1732.jpg", "/mnt/sdcard/DCIM/Camera");

Delete file from device

adbClient.DeleteFile("/sdcard/tmp/MP3Tube_v1.0_apkpure.com.apk");

Install application

Install application to the internal memory:

adbClient.InstallApplication(@"C:\Temp\MP3Tube_v1.0_apkpure.com.apk", false);

Install application to the SD card:

adbClient.InstallApplication(@"C:\Temp\MP3Tube_v1.0_apkpure.com.apk", true);

Uninstall application

Uninstall application and delete data and cache directories:

adbClient.UninstallApplication("angel.engmp3tube");

Uninstall application but keep data and cache directories:

adbClient.UninstallApplication("angel.engmp3tube", true);

List installed applications

var apps = adbClient.GetInstalledApplications();
foreach (var app in apps)
{
    Console.WriteLine($"{app.Name}\t{app.Location}\t{app.Type}\t{app.FileName}");
}

adbdotnet's People

Contributors

vurdalakov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.