UWP and WinUI - How to check my OS version through C#

Anderson Rodrigues Cavalcante 271 Reputation points
2024-02-22T00:06:54.7766667+00:00

Hello. I'd like to check the user Operational System through C# in UWP and WinUI. In my case, I'd like a method to return if the OS is Windows 10 or 11. I've tried:
var os = Environment.OSVersion; OperatingSystem opSys = new OperatingSystem(os.Platform, os.Version);

But the Version is Win32NT for Windows 10 and 11 in both cases.

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 15,676 Reputation points Microsoft Vendor
    2024-02-22T01:52:38.8066667+00:00

    Hello @Anderson Rodrigues Cavalcante ,

    Welcome to Microsoft Q&A!

    It is recommended to use Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamilyVersion, you can get OS build information from it.

    The OS build of Win11 starts from 22000. If the build is smaller than 22000, it can determine that the current OS is Win10.

    string deviceFamilyVersion = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamilyVersion;
    ulong version = ulong.Parse(deviceFamilyVersion);
    ulong major = (version & 0xFFFF000000000000L) >> 48;
    ulong minor = (version & 0x0000FFFF00000000L) >> 32;
    ulong build = (version & 0x00000000FFFF0000L) >> 16;
    ulong revision = (version & 0x000000000000FFFFL);
    var osVersion = $"{major}.{minor}.{build}.{revision}";
                
    if (build >= 22000)
    {
        Debug.WriteLine("win11");
    }
    else
    {
        Debug.WriteLine("win10");
    }
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful