When programming in WMI one typicall performs a WMI Query like "Select * from Win32_BIOS". The return is typically loaded into a collection and then we loop through the collection in a statement something along the lines of "For each objItem in wmiCollection". The properties would then become (in the case of the BIOS serial number) objItem.SerialNumber.
I'd like to query specific information and assign it directly to a variable with out needlessly looping through a collection of properties I am going to discard anyway. Is there a way to skip the whole collection assignment / For/ Next loop ?
The code I am using looks like this (straight from the WMI Code Generation tool from M$)
Try
Dim connection As New ConnectionOptions
connection.Username = userNameBox.Text
connection.Password = passwordBox.Text
connection.Authority = "ntlmdomain:DOMAIN"
Dim scope As New ManagementScope( _
"\\FullComputerName\root\C
IMV2", connection)
scope.Connect()
Dim query As New ObjectQuery( _
"SELECT * FROM Win32_BIOS")
Dim searcher As New ManagementObjectSearcher(s
cope, query)
For Each queryObj As ManagementObject In searcher.Get()
Console.WriteLine("-------
----------
----------
--------")
Console.WriteLine("Win32_B
IOS instance")
Console.WriteLine("-------
----------
----------
--------")
Console.WriteLine("SerialN
umber: {0}", queryObj("SerialNumber"))
Next
Close()
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessE
xception
MessageBox.Show("Connectio
n error (user name or password might be incorrect): " & unauthorizedErr.Message)
End Try
What I'd like to do is ditch the whole For Next loop and assign my end result directly to a variable; Where the Query would be "SELECT SerialNumber from Win32BIOS"..... I'm creating a XML Driven routine and am only after exacting properties.
I'm VERY new to VB.NET. And even newer to trying to work with WMI through VB.NET.
Start Free Trial