public bool IsDeviceAttached() { var query = "Select * From " + ClassId + " Where DeviceID = \"" + DeviceId.Replace("\\", "\\\\") + "\""; using (var searcher = new ManagementObjectSearcher(query)) { using (var collection = searcher.Get()) { return(collection.Count > 0); } } }
public Bitmap ScreenShot(string FilePath = null, bool deleteInAndroid = true) { bool IsDelete = false; if (string.IsNullOrEmpty(FilePath)) { FilePath = (string.IsNullOrEmpty(DeviceId) ? Guid.NewGuid().ToString() : DeviceId.Replace(":", "_")) + ".png"; IsDelete = true; } string androidPath = $"/sdcard/{Guid.NewGuid()}.png"; AdbCommand($"shell screencap -p \"{androidPath}\""); PullFile(androidPath, FilePath); if (deleteInAndroid) { DeleteFile(androidPath); } if (File.Exists(FilePath)) { try { byte[] buff = File.ReadAllBytes(FilePath); MemoryStream memoryStream = new MemoryStream(buff); return((Bitmap)Bitmap.FromStream(memoryStream)); } finally { if (IsDelete) { try { File.Delete(FilePath); } catch (Exception) { } } } } throw new FileNotFoundException(FilePath); }