我正在尝试将PC中的媒体设备启用。我可以使用WMI来获取设备列表,但是它似乎不是识别设备是否已启用的属性(我指的是设备管理器中的设备状态)。
我目前正在使用此代码来获取运行良好的设备列表。但是,如果用户已禁用该设备,则该列表仍然会返回该设备并且我的应用程序尝试使用它,但显然无法使用...
private static ManagementObjectCollection GetMediaDevices()
{
ManagementObjectSearcher objSearcher =
new ManagementObjectSearcher("SELECT HardwareID FROM Win32_PnPSignedDriver Where DeviceClass = 'MEDIA'");
return objSearcher.Get();
}
我查看了所有属性(SELECT *
),但似乎没有这些信息。
有什么想法吗?
答案 0 :(得分:0)
我有一些这样的代码段。
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()
或&#34;%COM%&#34; to captionLikeCondition参数。
如果启用了信息,请添加到条件 ConfigManagerErrorCode = 0 的位置 另见https://msdn.microsoft.com/en-us/library/aa394353%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396