获取驱动程序状态始终为null。为什么?

时间:2015-10-27 06:24:00

标签: c# wmi

我使用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
}

由于某种原因,'状态'永远是空的。我在Windows 10上以管理员身份运行。

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为你的做法是错误的。你为什么要去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;
        }
    }
}

btw,使用此对象,您可以启动,停止,暂停驱动程序