WMI call to get drivers

匆匆过客 提交于 2019-12-23 19:17:56

问题


I am new using WMI. What is it?

Can I use WMI call in C# for example to get list of drivers on my PC? If so, which class do I call?


回答1:


To list the installed drivers you can use the Win32_PnPSignedDriver WMI class as is show on this sample.

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {


        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PnPSignedDriver");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                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

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

Also if you are new in the WMI topic you can use a tool like the WMI Delphi Code Creator to explore the WMI contents and generate code to access the WMI.



来源:https://stackoverflow.com/questions/13132674/wmi-call-to-get-drivers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!