Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

861
Vistas
C# ¿cómo saber si el disco extraíble es una unidad USB o una tarjeta SD?

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!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda