C# – USB port current value

cnetusb

Is it possible to get the USB port current value when I plug the device in it?

The value should be available programmatically using C#. The second parameter I need is 'USB save power mode'. The correct name of the property is "Allow the computer to turn off this device to save power".

I managed to detect the device (if it is connected or disconnected) and read data from it correctly.

Do these two properties belong to USB device or to USB port?!

WinUSB Api did not give me clear answers to my questions.

Best Answer

you can use the WMI the get the USB port properties

public class MyClass
{
    public static void Main()
    {
        var usbDevices = GetUSBDevices();

        foreach (var usbDevice in usbDevices)
        {
            Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
        }
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        var searcher = new ManagementObjectSearcher(@"Select * From Win32_SerialPort");

        foreach (var device in searcher.Get())
        {
            devices.Add(new USBDeviceInfo(
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description"),
            (string)device.GetPropertyValue("Name")
            ));
        }

        return devices;
    }

    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
            this.Name = name;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
        public string Name { get; private set; }

    }
}

have a look at the below for the properties list

Availability should contain the info you are looking for

http://msdn.microsoft.com/en-us/library/aa394413%28v=vs.85%29.aspx