Compartir a través de


Using System.Management to get information about the Operating System from your VB.NET Application

I blogged about how to find Operating System Information from your VB.NET 2005 application last year in March. I found that Nilesh has posted a very nice reply to that post to ensure that the output of that program is even better.

Initially, if you used my code listing, you would have seen an output like...

On XP box with SP 2, you should get... Platform String = Microsoft Windows NT 5.1.2600 Service Pack 2
On Win 2k3 box with SP 1, you should get... Platform String = Microsoft Windows NT 5.2.3790 Service Pack 1

With the code that Nilesh shared with me, now you should be able to look at a much cleaner output, and not just Windows NT with some Number as version. Nilesh, if you are reading this, thanks a lot for sharing that code!

Create a new class called FindOSInfo and paste the following...

Imports System.Management
Public Class ClientInfo
Private objOS As ManagementObjectSearcher
Private objCS As ManagementObjectSearcher
Private objMgmt As ManagementObject
Private m_strComputerName As String
Private m_strManufacturer As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String
Public Sub New()
    objOS = New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
    objCS = New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem")
    For Each objMgmt In objOS.Get
        m_strOSName = objMgmt("name").ToString()
        m_strOSVersion = objMgmt("version").ToString()
        m_strComputerName = objMgmt("csname").ToString()
        m_strWindowsDir = objMgmt("windowsdirectory").ToString()
    Next
    For Each objMgmt In objCS.Get
        m_strManufacturer = objMgmt("manufacturer").ToString()
        m_StrModel = objMgmt("model").ToString()
        m_strSystemType = objMgmt("systemtype").ToString
        m_strTPM = objMgmt("totalphysicalmemory").ToString()
    Next
End Sub
Public ReadOnly Property ComputerName()
Get
    ComputerName = m_strComputerName
End Get
End Property
Public ReadOnly Property Manufacturer()
Get
    Manufacturer = m_strManufacturer
End Get
End Property
Public ReadOnly Property Model()
Get
    Model = m_StrModel
End Get
End Property
Public ReadOnly Property OsName()
Get
    OsName = m_strOSName
End Get
End Property
Public ReadOnly Property OSVersion()
Get
    OSVersion = m_strOSVersion
End Get
End Property
Public ReadOnly Property SystemType()
Get
    SystemType = m_strSystemType
End Get
End Property
Public ReadOnly Property TotalPhysicalMemory()
Get
    TotalPhysicalMemory = m_strTPM
End Get
End Property
Public ReadOnly Property WindowsDirectory()
Get
    WindowsDirectory = m_strWindowsDir
End Get
End Property
End Class

Now, create a Windows Form and drop a button on it. Call the following function from the button's click event as DisplayOperatingSystemInformation()

Sub DisplayOperatingSystemInformation()
  Dim osInfo As New FindOSInfo
  'Display Version Information
  MessageBox.Show( _
  "Computer Name = " & osInfo.ComputerName & vbCrLf & _
  "Manufacturer = " & osInfo.Manufacturer & vbCrLf & _
  "Model = " & osInfo.Model & vbCrLf & _
  "OS Name = " & osInfo.OsName & vbCrLf & _
  "Version = " & osInfo.OSVersion & vbCrLf & _
  "System Type = " & osInfo.SystemType & vbCrLf & _
  "Physical Memory = " & osInfo.TotalPhysicalMemory & vbCrLf & _
  "Windows Folder = " & osInfo.WindowsDirectory, _
  "Operating System Information", MessageBoxButtons.OK, _
  MessageBoxIcon.Information)
End Sub

I ran the same program on a Windows 2k3 box and a Vista box. Here is how the output looks like...

Hope that helps!
Rahul

kick it on DotNetKicks.com 

Share this post : email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

Comments

  • Anonymous
    March 07, 2007
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    July 04, 2007
    Hi there, This import looks like It doesn't exist in my libraries imports system.management Thank you

  • Anonymous
    February 11, 2008
    This import is not exist in my library. imports system.management it is giving error "This is not a public member or can not be found." Please tell us another way to do it Thank you

  • Anonymous
    February 11, 2008
    Sorry, Code is running. Just need to add reference of system.management then import it. Thank you