Problems Detecting whether i use Windows 10 or not, using C# and WPF?

windows

(afree) #1

i used the classic method described in this question:

checking which version of Windows you have using C#?

To detect whether i am using Windows 10 or not, but seems like there is some kind of BUG in windows, that instead of returning the correct windows Version number it returns the wrong version number "6.2.9200" instead of returning "10.0" as that table states.

is there any workaround, to check whether or not i have Windows 10 on my machine using C# ?


(Yassine) #2

That is really unexplained behavior at Microsoft documentations, I wish there is someone from Microsoft in here, to probably suggest a more “official” workaround.

As I believe that’s a common issue in Windows 8.1 and Windows 10. Environment.OSVersion Always returns the wrong version number on those two versions.

But anyways here is my suggested “unofficial” solution to it:

Windows Stores the current OS version friendly name in Registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName

So you can easily read it using regular registry classes and parse it, Something like this should do the trick:

using Microsoft.Win32;
 
/// Helper method to check if the currently installed OS version name
/// contains the given string
public bool IsCurrentOSContains(string name)
{
    var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
    string productName = (string)reg.GetValue("ProductName");
    
    return productName.Contains(name);
}
 
/// Check if it's Windows 8.1
public void IsWindows8Dot1()
{
    return IsCurrentOSContains("Windows 8.1");
}
 
/// Check if it's Windows 10
public void IsWindows10()
{
    return IsCurrentOSContains("Windows 10");
}

Other than this method i don’t know any other method that might work for you.


(afree) #3

Thanks for your assistance, really good trick, it’s really weird why Microsoft didn’t get this fixed and working properly.