[C#.NET][VB.NET] 如何利用 ManagementObjectSearcher 列舉 WMI 類別

[C#.NET][VB.NET] 如何利用 ManagementObjectSearcher 列舉 WMI 類別

1.需要加入System.Management參考,以及引用System.Management類別

2.利用 ManagementObjectSearcher 搜尋WMI類別

ManagementObjectSearcher Query = new ManagementObjectSearcher(strQuery);

3.利用 ManagementObjectCollection 擷取各種管理物件集合

ManagementObjectCollection EnumWMI = Query.Get();

C#完整範例

        private void button1_Click(object sender, EventArgs e)

        {

            long i = 0;

            this.listBox1.Items.Clear();

            //1.定義問句

            String strQuery = "select * from meta_class";

            //2.引用ManagementObjectSearcher

            ManagementObjectSearcher Query = new ManagementObjectSearcher(strQuery);

            //3.擷取各種WMI集合

            ManagementObjectCollection EnumWMI = Query.Get();

            //4.列舉WMI類別

            foreach (ManagementObject searcher in EnumWMI)

            {

                i++;

                this.listBox1.Items.Add(searcher.ToString());

            }

            this.label1.Text = i.ToString();

        }

 

 

 

VB完整範例

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Long = 0

        Me.ListBox1.Items.Clear()

        '1.定義問句

        Dim strQuery As [String] = "select * from meta_class"

        '2.引用ManagementObjectSearcher

        Dim Query As New ManagementObjectSearcher(strQuery)

        '3.擷取各種WMI集合

        'ManagementObjectCollection EnumWMI = Query.Get();

        Dim EnumWMI As ManagementObjectCollection = Query.[Get]()

        '4.列舉WMI類別

        For Each searcher As ManagementObject In EnumWMI

            i += 1

            Me.ListBox1.Items.Add(searcher.ToString())

        Next

        Me.Label1.Text = i.ToString()

    End Sub

快照-20097634619

4.範例下載:EnumWMI.rar

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo