【问题标题】:How to get list of ENABLED (media) devices in C#如何在 C# 中获取启用(媒体)设备的列表
【发布时间】:2015-09-08 01:30:54
【问题描述】:

我正在尝试在已启用的 PC 中获取媒体设备。我可以使用 WMI 来获取设备列表,但是似乎没有标识设备是否启用的属性(我指的是设备管理器中的设备状态)。

我目前正在使用此代码来获取运行良好的设备列表。但是,如果用户禁用了设备,该列表仍然会返回它,我的应用程序会尝试使用它,但显然无法使用...

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

我查看了所有属性 (SELECT *),但似乎都没有这些信息。

有什么想法吗?

【问题讨论】:

    标签: c# wmi device-manager


    【解决方案1】:

    我得到了一些这样的 sn-p 代码。

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

    并调用

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

    或 "%COM%" 到标题LikeCondition 参数。

    如果您获得启用信息,请添加到条件 ConfigManagerErrorCode = 0 另见https://msdn.microsoft.com/en-us/library/aa394353%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2022-07-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多