2010-09-10 21 views
5

Estoy tratando de hacer una función que detecta si un dispositivo usb está conectado dado los dispositivos pid y vid. Espero que se vea algo así, no estoy seguro de cómo hacer esto en C#.Dispositivo USB conectado

public bool IsUsbDeviceConnected(string pid, string vid) 
{ 
    //Code here 
} 
+2

Por favor, edite su pregunta; agregar los siguientes puntos puede brindarle mejores respuestas: 1. ¿Qué ha intentado hasta ahora? 2. ¿Qué resultados obtuviste? 3. ¿Cómo difería eso de los resultados que esperabas? – Piskvor

Respuesta

5
//using System.Management 
public bool IsUsbDeviceConnected(string pid, string vid) 
{ 
    using (var searcher = 
    new ManagementObjectSearcher(@"Select * From Win32_USBControllerDevice")) 
    { 
    using (var collection = searcher.Get()) 
    { 
     foreach (var device in collection) 
     { 
     var usbDevice = Convert.ToString(device); 

     if (usbDevice.Contains(pid) && usbDevice.Contains(vid)) 
      return true; 
     } 
    } 
    } 
    return false; 
} 
+0

¿Puede ayudarme con [este] (http://stackoverflow.com/q/7314257/75500) pregunta relacionada? – Shimmy

3

puede ser algo como

//import the System.Management namespace at the top in your "using" statement. Then in a method, or on a button click: 

ManagementObjectCollection collection; 
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'")) 
    collection = searcher.Get(); 
foreach (ManagementObject currentObject in collection) 
{ 
    ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'"); 
    MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString()); 
} 
collection.Dispose(); 

mediante WMI

+0

No tengo un número de serie, solo la identificación del proveedor y la identificación del producto que está anidada en algún lugar del usb. Además, esa llamada de WMI busca Win32_DiskDrives, no todos los dispositivos USB. – Robert

Cuestiones relacionadas