Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

862
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda