Know if a USB device is being used in windows with C #

2

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;
        }

Full Code

    
asked by felix0000 20.12.2016 в 16:33
source

0 answers