/// <summary> /// Find and return device by description. Only return device which connected as MTP / PTP. /// </summary> /// <param name="deviceDescription">Device description</param> /// <returns></returns> public MediaDevice GetDevice(string deviceDescription) { MediaDevice ptrDevice = GetDevices().FirstOrDefault(x => x.Description == deviceDescription); if (ptrDevice == null) { return(null); } //Enumerate Directory to ensure Media Device in MTP or PTP Mode. //By default, Android device is connected as USB Charging only with no file access. ptrDevice.Connect(); MediaDirectoryInfo rootInfo = ptrDevice.GetRootDirectory(); try { var dirs = rootInfo.EnumerateDirectories(); if (rootInfo.EnumerateDirectories().Count() > 0) { return(ptrDevice); } } catch { ptrDevice.Dispose(); ptrDevice = null; } return(null); }
private static int RunCommand(InfoOption opts) { using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts)) { if (device != null) { Console.WriteLine($"Device Friendly Name is {device.FriendlyName}"); Console.WriteLine($"Device Manufacturer is {device.Manufacturer}"); device.Connect(); Console.WriteLine($"Device Serial Number is {device.SerialNumber}"); var root = device.GetRootDirectory(); Console.WriteLine($"Root Directory is {root.FullName}"); Console.WriteLine("Drives:"); foreach (var drv in device.GetDrives()) { Console.WriteLine($"{drv.DriveType} \"{drv.RootDirectory.FullName}\" free {Extensions.SizeSuffix(drv.AvailableFreeSpace)} of {Extensions.SizeSuffix(drv.TotalSize)}"); } foreach (var item in device.GetContentLocations(ContentType.Certificate)) { Console.WriteLine(item); } device.Disconnect(); } else { Console.WriteLine("No device connected."); return(1); } } return(0); }
public MTPDevice(MediaDevice dev) { device = dev; device.Connect(); try { //Console.WriteLine("Connecting to " + device.FriendlyName); if (!device.FriendlyName.EndsWith(":\\")) //that means it's already mounted as a drive. don't trust it. { foreach (string a in device.GetDirectories("/")) { if (device.DirectoryExists(a + "/PSP/SAVEDATA")) { saveDirs.Add(new MTPSaveDir(this, a + "/PSP/SAVEDATA", $"[MTP:{device.FriendlyName}{a}] Android (or PSP via MTP)")); } if (device.DirectoryExists(a + "/switch/ppsspp/config/ppsspp/PSP/SAVEDATA")) { saveDirs.Add(new MTPSaveDir(this, a + "/switch/ppsspp/config/ppsspp/PSP/SAVEDATA", $"[MTP:{device.FriendlyName}{a}] Nintendo Switch")); } } } //device.Disconnect(); } catch (System.Runtime.InteropServices.COMException) { saveDirs.Clear(); } }
public void Connect() { var devices = MediaDevice.GetDevices(); _connectedDevice = devices.FirstOrDefault((device) => (device.FriendlyName == _deviceName) || (device.Description == _deviceName)); if (_connectedDevice == null) { var exception = new IOException($"Unable to connect to Garmin device: {_deviceName}"); Logger.Error("Connecting to the device problem.", exception); throw exception; } _connectedDevice.Connect(); }
private static int RunCommand(DirOption opts) { using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts)) { if (device != null) { Console.WriteLine($"Device Friendly Name is {device.FriendlyName}"); Console.WriteLine($"Device Manufacturer is {device.Manufacturer}"); device.Connect(); Console.WriteLine($"Device Serial Number is {device.SerialNumber}"); Console.WriteLine(); int fils = 0; int dirs = 0; ulong size = 0; try { foreach (var item in device.EnumerateDirectories(opts.Path)) { dirs++; var info = device.GetDirectoryInfo(item); Console.WriteLine($"{info.LastWriteTime:g} <DIR> {info.Name}"); } foreach (var item in device.EnumerateFiles(opts.Path)) { fils++; var info = device.GetFileInfo(item); size += info.Length; Console.WriteLine($"{info.LastWriteTime:g} {info.Length,15:N0} {info.Name}"); } Console.WriteLine($"{fils,3} File(s) {size:N0} Bytes"); Console.WriteLine($"{dirs,3} Dir(s)"); } catch (System.IO.DirectoryNotFoundException ex) { Console.WriteLine(ex.Message); } device.Disconnect(); } else { Console.WriteLine("No device connected."); return(1); } } return(0); }
private static int RunCommand(GetOption opts) { using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts)) { if (device != null) { device.Connect(); device.DownloadFile(opts.Source, opts.Destination); device.Disconnect(); } else { Console.WriteLine("No device connected."); return(1); } } return(0); }
private void Button2_Click(object sender, EventArgs e) { if (Device == null) { Device = Devices.ElementAt(comboBox1.SelectedIndex); Device.Connect(); Device.DeviceRemoved += Device_DeviceRemoved; button2.Text = "Disconnect"; Drives = Device.GetDrives(); foreach (MediaDriveInfo d in Drives) { comboBox2.Items.Add(d.VolumeLabel); } } else if (Device.IsConnected) { Device.Disconnect(); Device = null; button2.Text = "Connect"; Drive = null; Drives = null; comboBox2.Items.Clear(); } else { Device = Devices.ElementAt(comboBox1.SelectedIndex); Device.Connect(); Device.DeviceRemoved += Device_DeviceRemoved; button2.Text = "Disconnect"; Drives = Device.GetDrives(); if (Drives == null) { return; } if (Drives.Length == 0) { return; } foreach (MediaDriveInfo d in Drives) { comboBox2.Items.Add(d.VolumeLabel); } } }
private static int RunCommand(PutOption opts) { using (MediaDevice device = MediaDevice.GetDevices().FirstOrDefault(opts)) { if (device != null) { device.Connect(); string destDir = Path.GetDirectoryName(opts.Destination); if (!device.DirectoryExists(destDir)) { device.CreateDirectory(destDir); } device.UploadFile(opts.Source, opts.Destination); device.Disconnect(); } else { Console.WriteLine("No device connected."); return(1); } } return(0); }
public MtpDeviceMusicService() { List <MediaDevice> mediaDevices = MediaDevice.GetDevices().ToList(); switch (mediaDevices.Count) { case 0: throw new DeviceException( "No attached MTP media devices found, make sure your Android phone's USB mode is set to MTP."); case 1: device = mediaDevices[0]; break; case int _ when mediaDevices.Count > 1: throw new DeviceException("Too many devices, not sure which one to use."); } device.Connect(); // this throws exceptions if the program is built for x64 // https://github.com/Bassman2/MediaDevices/issues/6 rootDirectory = device.GetRootDirectory().EnumerateDirectories().First().Name; }