Search code examples
c#operating-systemwindows-10version

Get current version OS in Windows 10 in C#


I Use C#. I try to get the current version of the OS:

OperatingSystem os = Environment.OSVersion;
Version ver = os.Version;

I get on the Windows 10: 6.2.

But 6.2 is Windows 8 or WindowsServer 2012 (Detect Windows version in .net)

I found the following solution (How can I detect if my app is running on Windows 10).

static bool IsWindows10()
{
  var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
  string productName = (string)reg.GetValue("ProductName");
  return productName.StartsWith("Windows 10");
}

This is the best way to get the current version in C#?


Solution

  • Add application manifest to your application and add the supportedOS Id of Windows 8.1 and Windows 10 to the manifest:

    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
            <application> 
                <!-- Windows 10 --> 
                <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
                <!-- Windows 8.1 -->
                <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            </application> 
        </compatibility>
    

    Now Environment.OSVersion includes the correct data for Windows 8.1 and Windows 10 and not 6.2 to indicate you run Windows 8. This is a change since Windows 8.1.