I'm trying to create a program that lets you know if an USB "X" device is being used regardless of the type. Attached is the link and part of the code that I use to obtain the list of devices.
Code:
public class Usb
{
/// <summary>
/// obtiene las usb de la computadora
/// </summary>
/// <returns></returns>
public List<USBInfo> GetUSBDevices()
{
//creamos una lista de USBInfo
List<USBInfo> lstDispositivos = new List<USBInfo>();
//creamos un ManagementObjectCollection para obtener nuestros dispositivos
ManagementObjectCollection collection;
//utilizando la WMI clase Win32_USBHub obtenemos todos los dispositivos USB
using (var searcher = new ManagementObjectSearcher
(@"Select * From Win32_USBHub"))
//asignamos los dispositivos a nuestra coleccion
collection = searcher.Get();
//recorremos la colección
foreach (var device in collection)
{
//asignamos el dispositivo a nuestra lista
lstDispositivos.Add(new USBInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
//liberamos el objeto collection
collection.Dispose();
//regresamos la lista
return lstDispositivos;
}