Microsoft KB Archive/304283

= How to determine the Windows version by using Visual C# =

Article ID: 304283

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q304283



For a Microsoft Visual Basic .NET version of this article, see 304289.

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

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, or Windows XP
 * 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 (Windows Me), Microsoft Windows NT 3.51, Microsoft Windows NT 4.0, Microsoft Windows 2000, and Microsoft Windows XP.

back to the top

Requirements

 * Microsoft Visual C# .NET
 * Microsoft Visual C# 2005
 * Intermediate level understanding of Visual C# 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 is verified for use with 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.
 * |Windows|Windows|Windows|Windows NT|Windows|Windows|
 * | 95   |  98   |  Me   |    4.0   | 2000  |  XP   |
 * PlatformID | 1    | 1     | 1     | 2        | 2     | 2     |
 * Major     |       |       |       |          |       |       |
 * version  | 4     | 4     | 4     | 4        | 5     | 5     |
 * Minor     |       |       |       |          |       |       |
 * version  | 0     | 10    | 90    | 0        | 0     | 1     |

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. System.OperatingSystem osInfo = System.Environment.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

switch(osInfo.Platform) {           case System.PlatformID.Win32Windows: {                // Code to determine specific version of Windows 95, // Windows 98, Windows 98 Second Edition, or Windows Me. }

case System.PlatformID.Win32NT: {                 // Code to determine specific version of Windows NT 3.51, // Windows NT 4.0, Windows 2000, or Windows XP. }        } 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 System.PlatformID.Win32Windows: switch (osInfo.Version.Minor) {           case 0: Console.WriteLine (&quot;Windows 95&quot;); break; case 10: if(osInfo.Version.Revision.ToString==&quot;2222A&quot;) Console.WriteLine(&quot;Windows 98 Second Edition&quot;); else Console.WriteLine(&quot;Windows 98&quot;); break; case 90: Console.WriteLine(&quot;Windows Me&quot;); break; }break; back to the top

Determine the Specific Version of Windows NT, Windows 2000, or Windows XP
If you determine that the platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, or Windows XP, 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, // or Windows XP. case System.PlatformID.Win32NT:

switch(osInfo.Version.Major) {           case 3: Console.WriteLine(&quot;Windows NT 3.51&quot;); break; case 4: Console.WriteLine(&quot;Windows NT 4.0&quot;); break; case 5: if (osInfo.Version.Minor==0) Console.WriteLine(&quot;Windows 2000&quot;); else Console.WriteLine(&quot;Windows XP&quot;); break; }break; back to the top

Build the Sample
The following steps build a test scenario that demonstrates this functionality:  In Visual Studio .NET, open a new C# console application. The code window for Class1.cs opens by default.

Note In Visual Studio 2005, the default file is Program.cs. You can double-click Program.cs to open the code window.  Replace all of the code in the Class1.cs code editor window with the following sample code: using System;

namespace determineOS_CS { class Class1 {     static void Main(string[] args) {        // Get OperatingSystem information from the system namespace. System.OperatingSystem osInfo =System.Environment.OSVersion; // Determine the platform. switch(osInfo.Platform) {           // Platform is Windows 95, Windows 98, // Windows 98 Second Edition, or Windows Me. case System.PlatformID.Win32Windows: switch (osInfo.Version.Minor) {                 case 0: Console.WriteLine (&quot;Windows 95&quot;); break; case 10: if(osInfo.Version.Revision.ToString==&quot;2222A&quot;) Console.WriteLine(&quot;Windows 98 Second Edition&quot;); else Console.WriteLine(&quot;Windows 98&quot;); break; case 90: Console.WriteLine(&quot;Windows Me&quot;); break; }              break; // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, // or Windows XP. case System.PlatformID.Win32NT:

switch(osInfo.Version.Major)

{                 case 3: Console.WriteLine(&quot;Windows NT 3.51&quot;); break; case 4: Console.WriteLine(&quot;Windows NT 4.0&quot;); break; case 5: if (osInfo.Version.Minor==0) Console.WriteLine(&quot;Windows 2000&quot;); else Console.WriteLine(&quot;Windows XP&quot;); break; }break; }        Console.ReadLine ; }  } }                     Press CTRL+F5 to run the application. Note that the Windows version appears in the console window.

back to the top

Keywords: kbhowtomaster kbprod2web KB304283

-

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

© Microsoft Corporation. All rights reserved.