C#: Detect Windows OS Version Info

Currently, several versions of Windows OS are available in the market from Windows XP to latest Windows 8. For one my application, I was looking for a way to determine the OS version of the installed Windows at my workstation using C#. I have googled for the answer but no answer give me a clear idea and full functional way to determine the OS version. So, I find out a way to determine the OS version of Windows in my application using C# and in this tutorial I would like to describe the ways to do that.


Concept:


Every released windows operating system has a version number.  The version number has two main part formatted as "x.x", first digit is referred as Major Version Number and second digit is referred as Minor Version number.

In the following table most recent Windows OS versions are summarised.

Operating system Version number
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 Service Pack 1 6.1
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 20005.0

In C#, a Property named System.Environment.OSVersion can be used to get the current platform identifier and version number of the Operating System.


using System;
 
class Sample
{
    public static void Main()
    {
    Console.WriteLine();
    Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
    }
}
/*
This example produces the following results for my Windows 7 PC:
 
OSVersion: Microsoft Windows NT 6.1.7600.0
*/
We could use some useful property variables to get OS Version values
  • System.Environment.OSVersion.Version.Major to get the Major version number
  • System.Environment.OSVersion.Version.Minor to get the Minor version number
Previous
Next Post »