Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

854
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!