7

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.

barneyAgumble
  • 83
  • 1
  • 5
  • its now more about compatibility and keeping things working than highlighting the latest windows version – Daniel A. White Sep 03 '21 at 01:35
  • 2
    Instead of detecting Windows 11, detect the feature you are trying to use. – Raymond Chen Sep 03 '21 at 02:07
  • 2
    @RaymondChen I respect you a lot and love your books and blog and quite honestly humbled you responded to my question, but that is a terrible answer. Windows is too big for that. – barneyAgumble Sep 03 '21 at 02:18
  • 1
    If you are checking for Windows 11 in order to decide whether to call some new Windows 11 API, then the thing to do is to check for the presence of the API you are thinking of calling. – Raymond Chen Sep 03 '21 at 02:35
  • 2
    In theory, sure. But there's more than just detected APIs. And with .NET that's not even easy to do. But also what about kernel features from user mode? How can my installer detect which driver to install? There are significant differences between Win 7, 8 and 10 (and probably 11) with kernel features that you basically have to compile separate drivers to target those features properly (WFP and minifilters come to mind). So I increase my complexity in the kernel or Microsoft could just make a simple API that returns 7, 8, 10 or 11 and I follow a simple user mode path. – barneyAgumble Sep 03 '21 at 03:10
  • @RaymondChen, in my case, the feature I'm using is a collection of virtual machines. When a problem report comes in, I need to know which one to spin up to try to reproduce the issue. – Mark Mar 14 '22 at 21:12
  • 1
    @Mark For that purpose, use `Windows.System.Profile.AnalyticsInfo.VersionInfo`. If you get a version that doesn't match the version of any of your VMs, then you know that you need to go create a new VM. – Raymond Chen Mar 14 '22 at 21:27

4 Answers4

4

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.

LW001
  • 2,217
  • 4
  • 27
  • 34
  • 3
    That's what I found too but still feels very weak. Windows 10 will be around for a lot longer so I don't see how the build numbers won't conflict. – barneyAgumble Sep 03 '21 at 02:21
  • I don't think they will, now that the actual release of 11 is a month away and releases of 10 have switched to twice a year, each having increased the build number by one (21H1 being 19043 and 20H2 being 19042) with only about 5 years to go I don't see them going over that with 10. – LW001 Sep 03 '21 at 02:37
  • 1
    I tested this code and it failed. It returned wrong build number – gil123 Dec 03 '21 at 17:24
  • It seems that it is safer to use `Environment.OSVersion.Version.Build = 22000;` for Windows 11, because `> 22000` will hit the next version of Windows at some point. – Maris B. May 24 '22 at 12:11
  • @gil123 - you must add the `supportedOS` GUIDs to your the manifest file to get the correct results. Read more here: https://docs.microsoft.com/en-us/windows/win32/sysinfo/targeting-your-application-at-windows-8-1 and https://stackoverflow.com/questions/26151534/whats-the-supportedos-guid-for-windows-10 – Maris B. May 24 '22 at 12:14
1

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.

0

You can use RtlGetVersion() in ntdll.dll, which is not subject to OS compatibility manifestation, like GetVersion/Ex() and other similar APIs are (and especially since there is no supportedOS GUID defined for Windows 11 yet).

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
  • I recognized that as well which makes me think that Microsoft doesn't consider Windows 11 a significant update from Windows 10 21H1/2. Basically just a yearly update but with a new name (along with seemingly arbitrary hardware restrictions added on). – barneyAgumble Sep 03 '21 at 02:54
  • 1
    Personally, In my code the version that works is Remy's answer. I am actually using stack of GetVersion()-> RtlGetVersion() -> kernel32.dll version. It tries to detect inconsistencies and gets best version. For Win10 in particular, version is still 10 but build is 22000 or superior. However not sure if that's enough information. What will be the next Win 10 build? Over 22000 or is now stuck to latest 19043? M$ mess as usual. Why make simple when they can make complicated. – Niki Oct 05 '21 at 09:40
0

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.

gil123
  • 357
  • 4
  • 11