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.
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}");
}
}
}
}
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);
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();
}
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}");
}
adbClient.SetDevice(devices[0].SerialNumber);
Console.WriteLine($"Connected to device {devices[0].SerialNumber}");
var props = adbClient.GetDeviceProperties();
Console.WriteLine($"{props.Count} properties:");
foreach (var prop in props)
{
Console.WriteLine($"{prop.Key}={prop.Value}");
}
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"]}");
var response = adbClient.ExecuteRemoteCommand("ls -l /mnt/sdcard/DCIM/Camera");
Console.WriteLine($"Result:\n{String.Join("\r\n", response)}");
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}");
}
var fileInfo = adbClient.GetFileInfo("/mnt/sdcard/DCIM/Camera/20161130_1732.jpg");
Console.WriteLine($"{fileInfo.Name} {fileInfo.Size} {fileInfo.Mode} {fileInfo.FullName}");
var fileName = adbClient.GetFileInfo("/mnt/sdcard/DCIM/Camera/20161130_1732.jpg");
adbClient.DownloadFile(fileInfo.FullName, @"C:\Temp\" + fileInfo.Name);
adbClient.UploadFile(@"C:\Temp:\20161130_1732.jpg", "/mnt/sdcard/DCIM/Camera");
adbClient.DeleteFile("/sdcard/tmp/MP3Tube_v1.0_apkpure.com.apk");
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 and delete data and cache directories:
adbClient.UninstallApplication("angel.engmp3tube");
Uninstall application but keep data and cache directories:
adbClient.UninstallApplication("angel.engmp3tube", true);
var apps = adbClient.GetInstalledApplications();
foreach (var app in apps)
{
Console.WriteLine($"{app.Name}\t{app.Location}\t{app.Type}\t{app.FileName}");
}