Microsoft KB Archive/304289

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

Article ID: 304289

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q304289



For a Microsoft Visual C# .NET version of this article, see 304283.

For a Microsoft Visual C++ .NET version of this article, see 307394.



For a Microsoft Visual Basic 6.0 version of this article, see 189249.

IN THIS TASK
SUMMARY
 * Requirements
 * Obtain the Windows Version Data
 * Obtain the Operating System Information
 * Determine the Platform
 * Determine the Specific Version of Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me
 * Determine the Specific Version of Windows NT, Windows 2000, Windows XP, or Windows Server 2003
 * Build the Sample



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.

back to the top

Requirements

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

back to the top

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

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.

back to the top

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 back to the top

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 back to the top

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 = &quot;Windows 95&quot; Case 10 If .Version.Revision.ToString = &quot;2222A&quot; Then getVersion = &quot;Windows 98 Second Edition&quot; Else getVersion = &quot;Windows 98&quot; End If       Case 90 getVersion = &quot;Windows Me&quot; End Select back to the top

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 = &quot;Windows NT 3.51&quot; Case 4 getVersion = &quot;Windows NT 4.0&quot; Case 5 Select Case (.Version.Minor) Case 0 getVersion = &quot;Windows 2000&quot; Case 1 getVersion = &quot;Windows XP&quot; Case 2 getVersion = &quot;Windows Server 2003&quot; End Select Case Else getVersion = &quot;Failed&quot; End Select back to the top

Build the Sample
The following steps build a test scenario that demonstrates this functionality:  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.  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 = &quot;Windows 95&quot; Case 10 If .Version.Revision.ToString = &quot;2222A&quot; Then getVersion = &quot;Windows 98 Second Edition&quot; Else getVersion = &quot;Windows 98&quot; End If                       Case 90 getVersion = &quot;Windows Me&quot; End Select

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

back to the top

Keywords: kbvs2005applies kbvs2005swept kbproperties kbhowtomaster KB304289

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.