How to determine the Windows version by using Visual C#

Article translations Article translations
Article ID: 304283 - View products that this article applies to.
This article was previously published under Q304283
Expand all | Collapse all

On This Page

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.

Requirements

  • Microsoft Visual C# .NET
  • Microsoft Visual C# 2005 or Visual C# 2008
  • Intermediate level understanding of Visual C# programming

Obtain the Windows Version Data

To determine the operating system that is running on a system, you must obtain the following data:
+--------------------------------------------------------------+
|           |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     |
+--------------------------------------------------------------+
				
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.

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;
				

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.
               }
           
         }
				

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 ("Windows 95");
                break;
            case 10:
                if(osInfo.Version.Revision.ToString()=="2222A")
                    Console.WriteLine("Windows 98 Second Edition");
                else
                     Console.WriteLine("Windows 98");
                     break;
            case  90:
                     Console.WriteLine("Windows Me");
                     break;
         }break;
				

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("Windows NT 3.51");
                break;
            case 4:
                Console.WriteLine("Windows NT 4.0");
                break;
            case 5:
                if (osInfo.Version.Minor==0) 
                    Console.WriteLine("Windows 2000");
                else
                    Console.WriteLine("Windows XP");
                    break;
         }break;
				

Build the Sample

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

    Note In Visual Studio 2005 or in Visual Studio 2008, the default file is Program.cs. You can double-click Program.cs to open the code window.
  2. 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 ("Windows 95");
                         break;
                      case 10:
                         if(osInfo.Version.Revision.ToString()=="2222A")
                            Console.WriteLine("Windows 98 Second Edition");
                         else
                            Console.WriteLine("Windows 98");
                         break;
                      case  90:
                         Console.WriteLine("Windows Me");
                         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("Windows NT 3.51");
                         break;
                      case 4:
                         Console.WriteLine("Windows NT 4.0");
                         break;
                      case 5:
                         if (osInfo.Version.Minor==0) 
                            Console.WriteLine("Windows 2000");
                         else
                            Console.WriteLine("Windows XP");
                         break;
                   }break;
             }
             Console.ReadLine ();
          }
       }
    }
    					
  3. Press CTRL+F5 to run the application. Note that the Windows version appears in the console window.

Properties

Article ID: 304283 - Last Review: July 15, 2008 - Revision: 3.0
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# 2008 Express Edition
Keywords: 
kbhowtomaster kbprod2web KB304283

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com