获取驱动程序状态始终返回null.为什么?
本文关键字:返回 null 为什么 程序状态 获取 | 更新日期: 2023-09-27 18:19:26
我使用WMI来获取系统中的所有驱动程序:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_PnPSignedDriver");
foreach (ManagementObject WmiObject in searcher.Get())
{
Console.WriteLine("{0,-35} {1,-40}", "ClassGuid", WmiObject["ClassGuid"]);// String
Console.WriteLine("{0,-35} {1,-40}", "DeviceClass", WmiObject["DeviceClass"]);// String
Console.WriteLine("{0,-35} {1,-40}", "DeviceID", WmiObject["DeviceID"]);// String
Console.WriteLine("{0,-35} {1,-40}", "DeviceName", WmiObject["DeviceName"]);// String
Console.WriteLine("{0,-35} {1,-40}", "Manufacturer", WmiObject["Manufacturer"]);// String
Console.WriteLine("{0,-35} {1,-40}", "Name", WmiObject["Name"]);// String
Console.WriteLine("{0,-35} {1,-40}", "Status", WmiObject["Status"]);// String
}
由于某些原因,"Status"始终为null。我在Windows 10上以管理员身份运行。
你知道我做错了什么吗?
我认为你的方法是错误的。为什么要转到Win32_PnPSignedDriver
?如果您查看Win32_SystemDriver
,您可以实现更多目标,它将根据您的意愿返回状态、状态和启动。使用下面的类示例,获取您需要的信息:
using System;
using System.Collections.ObjectModel;
using System.Management;
using System.Windows;
namespace Agent.cls
{
public class Hw
{
public int DeviceId;
public bool AcceptPause;
public bool AcceptStop;
public string Caption;
public string CreationClassName;
public string Description;
public bool DeskTopInteract;
public string DisplayName;
public string ErrorControl;
public int ExitCode;
public DateTime InstallDate;
public string Name;
public string PathName;
public int ServiceSpecificExitCode;
public string ServiceType;
public bool started;
public string StartMode;
public string StartName;
public string State;
public string Status;
public string SystemCreationClassName;
public string SystemName;
public int TagId;
public static ObservableCollection<Hw> GetDevices()
{
var deviceList = new ObservableCollection<Hw>();
try
{
var query = new SelectQuery("Win32_SystemDriver");
var seracher = new ManagementObjectSearcher(query);
var counter = 0;
foreach (var wmiObject in seracher.Get())
{
var newDevice = new Hw
{
DeviceId = counter,
AcceptPause = Convert.ToBoolean(wmiObject["AcceptPause"]),
AcceptStop = Convert.ToBoolean(wmiObject["AcceptStop"]),
Caption = Convert.ToString(wmiObject["Caption"]),
CreationClassName = Convert.ToString(wmiObject["CreationClassName"]),
Description = Convert.ToString(wmiObject["Description"]),
DeskTopInteract = Convert.ToBoolean(wmiObject["DeskTopInteract"]),
DisplayName = Convert.ToString(wmiObject["DisplayName"]),
ErrorControl = Convert.ToString(wmiObject["ErrorControl"]),
ExitCode = Convert.ToInt16(wmiObject["ExitCode"]),
InstallDate = Convert.ToDateTime(wmiObject["InstallDate"]),
Name = Convert.ToString(wmiObject["Name"]),
PathName = Convert.ToString(wmiObject["PathName"]),
ServiceSpecificExitCode = Convert.ToInt16(wmiObject["ServiceSpecificExitCode"]),
ServiceType = Convert.ToString(wmiObject["ServiceType"]),
started = Convert.ToBoolean(wmiObject["Started"]),
StartMode = Convert.ToString(wmiObject["StartMode"]),
StartName = Convert.ToString(wmiObject["StartName"]),
State = Convert.ToString(wmiObject["State"]),
Status = Convert.ToString(wmiObject["Status"]),
SystemCreationClassName = Convert.ToString(wmiObject["SystemCreationClassName"]),
SystemName = Convert.ToString(wmiObject["SystemName"]),
TagId = Convert.ToInt16(wmiObject["TagId"])
};
deviceList.Add(newDevice);
counter++;
}
}
catch (Exception e)
{
MessageBox.Show("Error in device list 'n'n" + e);
}
return deviceList;
}
}
}
顺便说一句,有了这个对象,你可以启动、停止、暂停驱动程序