How to determine Windows version by using Visual Basic .NET or Visual Basic 2005

Summary

This step-by-step article demonstrates how to determine which operating system is in use on the system where your application is running. This article differentiates between Microsoft Windows 95, Microsoft Windows 98, Microsoft Windows 98 Second Edition, Microsoft Windows Millennium Edition (Me), Microsoft Windows NT 3.51, Microsoft Windows NT 4.0, Microsoft Windows 2000, Microsoft Windows XP, and Microsoft Windows Server 2003.

Requirements

  • Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
  • Intermediate level understanding of Visual Basic programming

Obtain the Windows Version Data

To determine the operating system that is running on a system, you must obtain the following data:

Windows 95Windows 98Windows MeWindows NT 4.0Windows 2000Windows XPWindows Server 2003
PlatformID 1 1 1 2 2 2 2
Major Version 4 4 4 4 5 5 5
Minor Version 0 10 90 0 0 1 2

NOTE: Although the code in this article checks for all 32-bit versions of Windows, Windows 95 and Windows NT 3.51 do not support Microsoft Visual Studio .NET or the common language runtime.

Obtain the Operating System Information

The System namespace contains a class named OperatingSystem. The properties for the OperatingSystem class provide the necessary information about the operating system that is in use. The OSVersion property of the System.Environment class returns an OperatingSystem object.
Private osInfo As OperatingSystem
osInfo = OSVersion

Determine the Platform

The first step in the logical evaluation of the OperatingSystem information is to determine which platform is in use. You can use the PlatformID property of the OperatingSystem class to determine which platform is in use.

For example, the enumerated value of the Win32Windows property indicates one of the following operating systems:
  • Windows 95
  • Windows 98
  • Windows 98 Second Edition
  • Windows Me
Similarly, the WinNT property indicates one of the following operating systems:
  • Windows NT 3.51
  • Windows NT 4.0
  • Windows 2000
  • Windows XP
  • Windows Server 2003
Select Case .Platform

Case .Platform.Win32Windows
'Code to determine specific version of Windows 95, Windows 98,
'Windows 98 Second Edition, or Windows Me.
End Select

Case .Platform.Win32NT
'Code to determine specific version of Windows NT 3.51, Windows NT 4.0,
'Windows 2000, Windows XP or Windows Server 2003.
End Select

End Select

Determine the Specific Version of Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me

If you determine that the platform is Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me, you can analyze the major or the minor version to determine the specific version.
'Platform is Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me.
Case .Platform.Win32Windows
Select Case (.Version.Minor)
Case 0
getVersion = "Windows 95"
Case 10
If .Version.Revision.ToString() = "2222A" Then
getVersion = "Windows 98 Second Edition"
Else
getVersion = "Windows 98"
End If
Case 90
getVersion = "Windows Me"
End Select

Determine the Specific Version of Windows NT, Windows 2000, Windows XP, or Windows Server 2003

If you determine that the platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, Windows XP, or Windows Server 2003, you can analyze the major or the minor version to determine the specific version.
'Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, Windows XP or Windows Server 2003.

Case .Platform.Win32NT
Select Case (.Version.Major)
Case 3
getVersion = "Windows NT 3.51"
Case 4
getVersion = "Windows NT 4.0"
Case 5
Select Case (.Version.Minor)
Case 0
getVersion = "Windows 2000"
Case 1
getVersion = "Windows XP"
Case 2
getVersion = "Windows Server 2003"
End Select
Case Else
getVersion = "Failed"
End Select

Build the Sample

The following steps build a test scenario that demonstrates this functionality:
  1. In Visual Studio .NET or in Visual Studio 2005, open a new Visual Basic console application. The code window for Module1.vb opens by default.
  2. Replace all of the code in the Module1.vb code editor window with the following code:
    Option Strict On
    Imports System.Environment
    Module Module1

    Private osInfo As OperatingSystem

    Sub Main()
    Console.WriteLine(getVersion())
    End Sub

    Public Function getVersion() As String

    osInfo = OSVersion
    With osInfo
    Select Case .Platform

    Case .Platform.Win32Windows
    Select Case (.Version.Minor)
    Case 0
    getVersion = "Windows 95"
    Case 10
    If .Version.Revision.ToString() = "2222A" Then
    getVersion = "Windows 98 Second Edition"
    Else
    getVersion = "Windows 98"
    End If
    Case 90
    getVersion = "Windows Me"
    End Select

    Case .Platform.Win32NT
    Select Case (.Version.Major)
    Case 3
    getVersion = "Windows NT 3.51"
    Case 4
    getVersion = "Windows NT 4.0"
    Case 5
    Select Case (.Version.Minor)
    Case 0
    getVersion = "Windows 2000"
    Case 1
    getVersion = "Windows XP"
    Case 2
    getVersion = "Windows Server 2003"
    End Select
    Case Else
    getVersion = "Failed"
    End Select
    End Select
    End With
    End Function
    End Module
  3. Press the CTRL+F5 key combination to run the application. Note that the Windows version appears in the console window.
Propriedades

ID do Artigo: 304289 - Última Revisão: 6 de dez de 2006 - Revisão: 1

Comentários