1

I am trying to get the media devices in a PC that are enabled. I can use WMI to get the list of devices, however there doesn't seem to be a property identifying if the device is enabled or not (I am referring to the devices' status in Device Manager).

I am currently using this code to get the list of devices which works well. However if the user has disabled the device, it is still returned by this list and my application tries to use it, but obviously cannot be used...

private static ManagementObjectCollection GetMediaDevices()
    {
        ManagementObjectSearcher objSearcher =
            new ManagementObjectSearcher("SELECT HardwareID FROM Win32_PnPSignedDriver Where DeviceClass = 'MEDIA'");
        return objSearcher.Get();
    }

I have looked at all the properties (SELECT *) but none seem to have this information.

Any ideas?

Simon
  • 9,197
  • 13
  • 72
  • 115

1 Answers1

0

I got some snippet codes like this.

public static List<DeviceCompactInfo> GetPnPDeviceInfo(string captionLikeCondition)
{
    var selectQuery = "SELECT Caption, Description, Manufacturer, SystemName, DeviceID From Win32_PnPEntity ";
    var query = string.Format("{0} WHERE ConfigManagerErrorCode = 0 and Caption like '{1}' ", selectQuery, captionLikeCondition);
    var searcher = new System.Management.ManagementObjectSearcher(query);
    var pnpList = searcher.Get().Cast<System.Management.ManagementBaseObject>()
        .Select(x => new DeviceCompactInfo
        {
            Name = Convert.ToString(x["Caption"]),
            Description = Convert.ToString(x["Description"]),
            Manufacturer = Convert.ToString(x["Manufacturer"]),
            SystemName = Convert.ToString(x["SystemName"]),
            DeviceID = Convert.ToString(x["DeviceID"]),
        })
        .ToList();

    return pnpList;
}

[System.Diagnostics.DebuggerDisplay("Name:{Name}, Description:{Description}, Manufacturer:{Manufacturer}, SystemName:{SystemName}, DeviceID:{DeviceID}", Name = "DeviceCompactInfo")]
public class DeviceCompactInfo
{
    public string Name
    {
        get;
        set;
    }

    public string Description
    {
        get;
        set;
    }

    public string Manufacturer
    {
        get;
        set;
    }

    public string SystemName
    {
        get;
        set;
    }

    public string DeviceID
    {
        get;
        set;
    }
}

and call

GetPnPDeviceInfo("%cam%").Select(x => x.Name).ToList()

or "%COM%" to captionLikeCondition parameter.

If you get enabled info add to where condition ConfigManagerErrorCode = 0 see also https://msdn.microsoft.com/en-us/library/aa394353%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

Kim Ki Won
  • 1,795
  • 1
  • 22
  • 22