.Net's Environment.OsVersion object gives us the version information of the OS we are running under - its Platform property gives us the OS family and we can then use the Version property to narrow down the specific OS.
This article from Microsoft outlines a function that detects the current OS version but Bj Rollison has a more up to date implementation that includes detection of Windows 2003 Server and Vista.
Below is my implementation that uses an Enum to identify the OS. You can access the Description attribute of the Enum to get a more readable string representation of the OS - I've included a helper method for this too :)
Jimmy
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;
namespace Utils
{
// custom enum of operating systems to allow for easy comparisons
public enum OSVersion
{
[Description("Unknown")]
Unknown,
[Description("Windows 95")]
Windows95,
[Description("Windows 95 Service Pack 1")]
Windows95SP1,
[Description("Windows 95 OSR 2.0")]
Windows95OSR20,
[Description("Windows 95 OSR 2.1")]
Windows95OSR21,
[Description("Windows 95 OSR 2.5")]
Windows95OSR25,
[Description("Windows 98")]
Windows98,
[Description("Windows 98 Second Edition")]
Windows98SE,
[Description("Windows ME")]
WindowsMe,
[Description("Windows NT 3.51")]
WindowsNT351,
[Description("Windows NT 4.0")]
WindowsNT4,
[Description("Windows 2000")]
Windows2000,
[Description("Windows XP")]
WindowsXP,
[Description("Windows 2003 Server")]
WindowsXP2K3,
[Description("Windows Vista")]
WindowsVista,
[Description("Windows Vista Service Pack 1")]
WindowsVistaSP1,
}
public static class OSHelper
{
// Major NT Kernel Versions
private const int WINDOWS_NT6_KERNEL = 6;
private const int WINDOWS_NT4_KERNEL = 4;
private const int WINDOWS_NT5_KERNEL = 5;
// Minor NT Kernel Versions
private const int WINDOWS_2000 = 0;
private const int WINDOWS_XP = 1;
private const int WINDOWS_XP2003 = 2;
// Service Packs
private const string SP1 = "Service Pack 1";
// Minor Win9x Kernel Versions
private const int WINDOWS_95 = 0;
private const string WINDOWS_95_SP1 = "950A";
private const int WINDOWS_95_OSR = 3;
private const string WINDOWS_95_OSR2X = "950B";
private const string WINDOWS_95_OSR25 = "950C";
private const int WINDOWS_98 = 10;
private const string WINDOWS_98SE = "2222A";
private const int WINDOWS_ME = 90;
///<summary>
/// Gets the version of the Operating System
///</summary>
/// <returns><see cref="System.String"/> </returns>
public static OSVersion GetOSVersion()
{
OperatingSystem osVersionInfo = Environment.OSVersion;
OSVersion ret = OSVersion.Unknown;
switch (osVersionInfo.Platform)
{
case PlatformID.Win32NT:
switch (osVersionInfo.Version.Major)
{
// Windows NT 4.0
case WINDOWS_NT4_KERNEL:
ret = OSVersion.WindowsNT4;
break;
case WINDOWS_NT5_KERNEL:
switch (osVersionInfo.Version.Minor)
{
// Windows 2000
case WINDOWS_2000:
ret = OSVersion.Windows2000;
break;
// Windows Xp
case WINDOWS_XP:
ret = OSVersion.WindowsXP;
break;
// Windows Xp 2003 Server
case WINDOWS_XP2003:
ret = OSVersion.WindowsXP2K3;
break;
}
break;
// Windows Vista and Server 2008
case WINDOWS_NT6_KERNEL:
if (osVersionInfo.ServicePack == SP1)
ret = OSVersion.WindowsVistaSP1;
else
ret = OSVersion.WindowsVista;
break;
}
break;
case PlatformID.Win32Windows:
switch (osVersionInfo.Version.Minor)
{
// Windows 95, OSR 1.0, OSR 2.0
case WINDOWS_95:
if (osVersionInfo.Version.Revision.ToString() == WINDOWS_95_SP1)
ret = OSVersion.Windows95SP1;
else if (osVersionInfo.Version.Revision.ToString() == WINDOWS_95_OSR2X)
ret = OSVersion.Windows95OSR20;
else
ret = OSVersion.Windows95;
break;
// Windows 95 OSR 2.1, OSR 2.5
case WINDOWS_95_OSR:
if (osVersionInfo.Version.Revision.ToString() == WINDOWS_95_OSR2X)
ret = OSVersion.Windows95OSR21;
else if (osVersionInfo.Version.Revision.ToString() == WINDOWS_95_OSR25)
ret = OSVersion.Windows95OSR25;
break;
// Windows 98 and Windows 98 Second Edition
case WINDOWS_98:
if (osVersionInfo.Version.Revision.ToString() == WINDOWS_98SE)
ret = OSVersion.Windows98SE;
else
ret = OSVersion.Windows98;
break;
// Windows ME
case WINDOWS_ME:
ret = OSVersion.WindowsMe;
break;
}
break;
}
return ret;
}
///<summary>
/// Allows the discovery of an OSVersion enumeration text value based on the <c>DescriptionAttribute</c>
///</summary>
/// <param name="e">The enum to get the reader friendly text value for.</param>
/// <returns><see cref="System.String"/> </returns>
public static string GetOSVersionDescription(OSVersion version)
{
string ret = String.Empty;
Type t = version.GetType();
MemberInfo[] members = t.GetMember(version.ToString());
if (members != null && members.Length == 1)
{
object[] attrs = members[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs.Length == 1)
{
ret = ((DescriptionAttribute)attrs[0]).Description;
}
}
return ret;
}
}
}