|
|
Title | Get operating system information in VB .NET |
Description | This example shows how to get operating system information including platform, OS, major version, minor version, build, and revision in Visual Basic .NET. It gets this information (after some interpretation) from Environment.OSVersion. |
Keywords | OS version, operating system version, VB.NET, system |
Categories | VB.NET, Windows |
|
|
The easiest thing to do is look at Environment.OSVersion.ToString() which returns the system's platform, major version, minor version, build, and revision number as in "Microsoft Windows NT 5.0.2195.0".
Unfortunately the platform (in this case, "Microsoft Windows NT") doesn't tell you the operating system. The following table lets you look up the operating system name based on the platform ID, major version, and minor version.
OS | Platform ID | Major Version | Minor Version |
Win3.1 | 0 | ? | ? |
Win95 | 1 | 4 | 0 |
Win98 | 1 | 4 | 10 |
WinME | 1 | 4 | 90 |
NT 3.51 | 2 | 3 | 51 |
NT 4.0 | 2 | 4 | 0 |
Win2000 | 2 | 5 | 0 |
WinXP | 2 | 5 | 1 |
Win2003 | 2 | 5 | 2 |
Vista/Windows Server 2008 | 2 | 6 | 0 |
(Thanks to Gary German for the WinXP and Win2003 info.)
(Thanks to Chris Millard for the Vista and Windows Server 2008 info. Visit his Web site.)
The GetOSVersion function shown in the following code uses this information to return the operating system name.
|
|
Public Function GetOSVersion() As String
Select Case Environment.OSVersion.Platform
Case PlatformID.Win32S
Return "Win 3.1"
Case PlatformID.Win32Windows
Select Case Environment.OSVersion.Version.Minor
Case 0
Return "Win95"
Case 10
Return "Win98"
Case 90
Return "WinME"
Case Else
Return "Unknown"
End Select
Case PlatformID.Win32NT
Select Case Environment.OSVersion.Version.Major
Case 3
Return "NT 3.51"
Case 4
Return "NT 4.0"
Case 5
Select Case _
Environment.OSVersion.Version.Minor
Case 0
Return "Win2000"
Case 1
Return "WinXP"
Case 2
Return "Win2003"
End Select
Case 6
Return "Vista/Win2008Server"
Case Else
Return "Unknown"
End Select
Case PlatformID.WinCE
Return "Win CE"
End Select
End Function
|
|
The following code shows how the example program uses function GetOSVersion and various Environment.OSVersion properties to get operating system version information.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
' Get the Environment.OSVersion.
Label1.Text = Environment.OSVersion.ToString()
' Get the interpreted version information.
lblPlatform.Text = _
Environment.OSVersion.Platform.ToString
lblOSVersion.Text = GetOSVersion()
lblMajor.Text = _
Environment.OSVersion.Version.Major.ToString
lblMinor.Text = _
Environment.OSVersion.Version.Minor.ToString
lblBuild.Text = _
Environment.OSVersion.Version.Build.ToString
lblRevision.Text = _
Environment.OSVersion.Version.Revision.ToString
End Sub
|
|
|
|
|
|