I using WMI in order to get all drivers in system:
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
}
For some reason, 'Status' is always null. I running as Administrator on Windows 10.
Any ideas what I doing wrong?
I think your approach is wrong. Why do you want to go to Win32_PnPSignedDriver
?
You can achieve more if you check out the Win32_SystemDriver
, which will return you the state, status and started as you wish. Use the following class example, to get the information you need:
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;
}
}
}
btw, with this object, you can start,stop,pause drivers