Detect Windows 11 with .NET Framework or Windows API

Viewed 4277

In .NET Framework, to get the OS version you can use Environment.OSVersion with the Major and Minor values telling you the version of Windows (i.e 6.1 = Windows 7, 10.0 = Windows 10). Even though Windows 11 has been released (dev and beta channels) for over a month now, the documentation has not been updated to mention how to detect Windows 11.

For Windows API, GetVersion has been deprecated forever and even the version helper APIs only go up to IsWindows10OrGreater. Is there a simple check to figure out the major Windows version, in particular Windows 11? Someone had a similar question but for Delphi (How to detect Windows 11 using Delphi 10.3.3) and the accepted answer was all hacks. Why the hell is it so difficult for Microsoft to provide a simple API to just return the current system version? GetVersion should never have been deprecated.

5 Answers

This most likely isn't perfect, but it's a workaround I've been using for a bit:

Environment.OSVersion.Version.Build >= 22000;

Windows 11 starts at build number 22000 and Windows 10 ends roughly at build number 21390, so this will only detect Windows 11 builds as far as I'm aware.

Do check whether it even is a Windows system in the first place though, another OS might have a higher build number.

RtlGetVersion() gave same results for Windows 10 and 11 as for me. I called it from the C# code as described here https://stackoverflow.com/a/49641055. I tried the Caption property of the WMI's Win32_OperatingSystem class.

using (var objOS = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
{
    foreach (ManagementObject objMgmt in objOS.Get())
    {
        Console.WriteLine("{0}: {1}", objMgmt.Properties["Caption"].Name, objMgmt.Properties["Caption"].Value);
    }
}

For Windows 10 I got "Microsoft Windows 10 Enterprise" and for Windows 11 "Microsoft Windows 11 Pro". Probably you could use the Name property but it contains excessive information like Windows folder and system drive or something like that.

Use this code:

public static bool IsWindows11()
{
    var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");

    var currentBuildStr = (string)reg.GetValue("CurrentBuild");
    var currentBuild = int.Parse(currentBuildStr);

    return currentBuild >= 22000;
}

This is based on my findings on HKLM\Software\Microsoft\Windows NT\CurrentVersion: What's the difference between CurrentBuild and CurrentBuildNumber?

And note that this code can throw exceptions in case that the registry key does not exist or in case the value is not number. This should not happen in a normal system. But if you want to be safe, you can warp it in a try-catch block.

Windows 10 start with build 10240 and end with build 19044
Windows 11 start with build 22000

Source

Related