3

我是使用 WMI 的新手。它是什么?

例如,我可以在 C# 中使用 WMI 调用来获取我的 PC 上的驱动程序列表吗?如果是这样,我应该打电话给哪个班级?

4

1 回答 1

6

要列出已安装的驱动程序,您可以使用Win32_PnPSignedDriverWMI 类,如本示例所示。

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();
        }
    }
}

此外,如果您是 WMI 主题的新手,您可以使用类似的工具WMI Delphi Code Creator来探索 WMI 内容并生成代码来访问 WMI。

于 2012-10-30T15:36:30.147 回答