2015-10-27 2 views
1

Я с помощью 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» всегда нуль. Я работаю как администратор в Windows 10.

Любые идеи, что я делаю неправильно?

+0

вы работаете в VS или EXE-файл в качестве администратора ? – pordi

+0

Я просто запросил Win32_PnpSignedDriver в моей системе Windows 10 с помощью PowerShell. Тот же результат: статус всегда равен нулю. Нет разницы при запуске в качестве администратора. – gdir

+0

@Babekoof - попробовал оба, тот же результат – Illidan

ответ

0

Я думаю, что ваш подход неправильный. Почему вы хотите пойти в 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; 
     } 
    } 
} 

Кстати, с этим объектом, вы можете запускать, останавливать, приостанавливать драйверы

Смежные вопросы