|
|
Title | Use WMI to get the operating system name including its edition, plus some other information in Visual Basic .NET |
Description | This example shows how to use WMI to get the operating system name including its edition, plus some other information in Visual Basic .NET. |
Keywords | WMI, operating system, version, 32-bit, 64-bit, Visual Basic .NET, VB.NET |
Categories | , Windows |
|
|
WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself. This example uses it to get:
- The operating system name including its edition (Home, Ultimate, etc.), version, and Service Pack number
- The number of logical processors
- The number of bits the system uses (32 or 64)
When the program loads, the following code displays these values in labels.
|
|
' Display the operating system's name.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Get the OS information.
' For more information from this query, see:
' http:'msdn.microsoft.com/library/aa394239.aspx
Dim os_query As String = "SELECT * FROM " & _
"Win32_OperatingSystem"
Dim os_searcher As New _
ManagementObjectSearcher(os_query)
For Each info As ManagementObject In os_searcher.Get()
lblCaption.Text = _
info.Properties("Caption").Value.ToString().Trim()
lblVersion.Text = "Version " & _
info.Properties("Version").Value.ToString() & _
" SP " & _
info.Properties("ServicePackMajorVersion").Value.ToString() _
& "." & _
info.Properties("ServicePackMinorVersion").Value.ToString()
Next info
' Get number of processors.
' For more information from this query, see:
' http:'msdn.microsoft.com/library/aa394373.aspx
Dim cpus_query As String = "SELECT * FROM " & _
"Win32_ComputerSystem"
Dim cpus_searcher As New _
ManagementObjectSearcher(cpus_query)
For Each info As ManagementObject In cpus_searcher.Get()
lblCpus.Text = _
info.Properties("NumberOfLogicalProcessors").Value.ToString() _
& _
" processors"
Next info
' Get 32- versus 64-bit.
' For more information from this query, see:
' http:'msdn.microsoft.com/library/aa394373.aspx
Dim proc_query As String = "SELECT * FROM " & _
"Win32_Processor"
Dim proc_searcher As New _
ManagementObjectSearcher(proc_query)
For Each info As ManagementObject In proc_searcher.Get()
lblBits.Text = _
info.Properties("AddressWidth").Value.ToString() _
& "-bit"
Next info
End Sub
|
|
The code uses the WMI query "SELECT * FROM Win32_OperatingSystem" to get information about the operating system. The result is a collection holding one Win32_OperatingSystem object. The code uses a loop to get that object and reads its properties. The Caption property gives the full operating system name including trademark symbols and the edition. Version returns the operating system's version. The ServicePackMajorVersion and ServicePackMinorVersion properties give the service pack information.
Next the program executes the query "SELECT * FROM Win32_ComputerSystem" in a similar fashion to get the number of logical processors.
It finishes by executing the query "SELECT * FROM Win32_Processor" to determine whether this is a 32-bit or 64-bit operating system.
Note that using WMI requires that you add a reference to System.Management.
|
|
|
|
|
|