Plataforma Windows 7, C#
Utilizo la siguiente declaración para enumerar todas las unidades:
DriveInfo[] drives = DriveInfo.GetDrives();entonces puedo usar DriveType para encontrar todos esos discos extraíbles:
foreach (var drive in drives) { if(drive.DriveType == DriveType.Removable) yield return drive; }ahora mi problema es que el disco de la tarjeta SD y el disco flash USB comparten el mismo tipo de unidad: extraíble, entonces, ¿cómo puedo encontrar solo el disco flash USB?
¡Gracias!
Puede aprovechar ManagementObjectSearcher usándolo para consultar las unidades de disco que son USB, luego obtener la letra de unidad correspondiente y devolver solo DriveInfo que RootDirectory.Name en el conjunto de resultados.
Uso de expresiones de consulta LINQ:
static IEnumerable<DriveInfo> GetUsbDevices() { IEnumerable<string> usbDrivesLetters = from drive in new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get().Cast<ManagementObject>() from o in drive.GetRelated("Win32_DiskPartition").Cast<ManagementObject>() from i in o.GetRelated("Win32_LogicalDisk").Cast<ManagementObject>() select string.Format("{0}\\", i["Name"]); return from drive in DriveInfo.GetDrives() where drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name) select drive; }Uso de métodos de extensión LINQ:
static IEnumerable<DriveInfo> GetUsbDevices() { IEnumerable<string> usbDrivesLetters = new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get().Cast<ManagementObject>() .SelectMany(drive => drive.GetRelated("Win32_DiskPartition").Cast<ManagementObject>()) .SelectMany(o => o.GetRelated("Win32_LogicalDisk").Cast<ManagementObject>()) .Select(i => Convert.ToString(i["Name"]) + "\\"); return DriveInfo.GetDrives().Where(drive => drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name)); }Usando foreach:
static IEnumerable<string> GetUsbDrivesLetters() { foreach (ManagementObject drive in new ManagementObjectSearcher("select * from Win32_DiskDrive WHERE InterfaceType='USB'").Get()) foreach (ManagementObject o in drive.GetRelated("Win32_DiskPartition")) foreach (ManagementObject i in o.GetRelated("Win32_LogicalDisk")) yield return string.Format("{0}\\", i["Name"]); } static IEnumerable<DriveInfo> GetUsbDevices() { IEnumerable<string> usbDrivesLetters = GetUsbDrivesLetters(); foreach (DriveInfo drive in DriveInfo.GetDrives()) if (drive.DriveType == DriveType.Removable && usbDrivesLetters.Contains(drive.RootDirectory.Name)) yield return drive; } Para usar ManagementObject , debe agregar una referencia a System.Management
No lo he probado bien porque ahora no tengo ninguna tarjeta SD, pero espero que ayude
Tuve que buscar dispositivos USB en un proyecto anterior y lo resolví así:
Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface; deviceInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE) string name = new string(deviceInterface.dbcc_name); Guid g = new Guid(deviceInterface.dbcc_classguid); if (g.ToString() == "a5dcbf10-6530-11d2-901f-00c04fb951ed") {*DO SOMETHING*}Obtengo el GUID y compruebo si el GUID del dispositivo es el USB-GUID.