5

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#?

Community
  • 1
  • 1
Olga
  • 1,395
  • 2
  • 25
  • 34

3 Answers3

10

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.

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
0

Here is a link from Microsoft offical, indicating how to get the System Version. It actually is a call to the Version API Helper Functions

So basically you must convert this code into C# because it's in C++, then keep only the Windows 10 part...

#include <windows.h>
#include <stdio.h>
#include <VersionHelpers.h>

int 
__cdecl
wmain(
    __in int argc, 
    __in_ecount(argc) PCWSTR argv[]
    )
{
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);
    if (IsWindows10OrGreater())
    {
        printf("Windows10OrGreater\n");
    }
}

And if you like trying to read code, you can check out this one link. This DLL can be used to get information on the OS...

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
0

I have created this simple method in C# and it has worked for me.

    public static string GetWindowsVersion()
    {
        string registryPath = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion";
        string build = null;
        int number = 0;

        try
        {
            build = Registry.GetValue(registryPath, "CurrentBuild", null).ToString();
        }
        catch { return null; }

        number = Int32.Parse(build);

        if (number == 7601)
            return "Windows 7";
        else if (number == 9200)
            return "Windows 8";
        else if (number == 9600)
            return "Windows 8.1";
        else if (number >= 10240 && number <= 19045)
            return "Windows 10";
        else if (number >= 22000)
            return "Windows 11";
        else
            return "Older version";

        /* Go here to find more build numbers and evaluate more conditions
         * 
         * https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
         * 
         */ 
    }