Search code examples
wmi-query

vb.net WMI query to string


I have a little issue with WMI query. I have to check if a certain property exists in a WMI query instance, the code i have done is:

Imports System.Management
Imports System.Management.Instrumentation

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim search_cpu As New ManagementObjectSearcher("SELECT * FROM Win32_Processor")
        Dim info_cpu As ManagementObject '= Convert.ToUInt32("search_cpu")
        Dim cpu_v As Integer
        For Each info_cpu In search_cpu.Get()
            If search_cpu.Get("caption") = True Then
                cpu_v = "Caption"
                Label1.Text = ("Name: " & info_cpu(cpu_v).ToString())
            End If
        Next
    End Sub
End Class

Any help will be appreciated. Thanks in advance


Solution

  • I managed to reproduce. The below code works allright for me and Label1 now displays:

    Intel64 Family 6 Model 58 Stepping 9

        Sub Main()
                Dim search_cpu As New ManagementObjectSearcher("SELECT * FROM Win32_Processor")
                Dim info_cpu As ManagementObject '= Convert.ToUInt32("search_cpu")
                Dim caption As String
                For Each info_cpu In search_cpu.Get()
                    caption = info_cpu("caption").ToString()
                Next
                Label1.Text = caption
                if (string.isnullorempty(caption))
                Label1.Text ="<does not exist>"
            End Sub