WPF - How to findout what version of Windows I have using C#?

csharp

(Kunni Can) #1

How can i find what version of Windows installed in my PC using C# code in a WPF project?


(Yassine) #2

I think this C# property is what you are looking for:

And by the way, it doesn’t make any difference whether calling it from windows Forms or WPF C# project, as long as you have access to the Environment Class.

The OSVersion Property provides all sort of details about your OS, platform (Unix, Windows, Mac…etc), Version Number, Name…etc.

Edit:
In case you need to check whether your Operating System is windows 8.1 or Windows 10, the code above won’t be stable enough, as Microsoft did some serious changes to the version-ing and it’s noted that the property above returns some inconsistent values, that sometimes refer to older windows versions (Windows 8).


(Kunni Can) #3

Unfortunately, i need it for Windows 10, and the OSVersion doesn’t seem to be consistent across different Windows PC’s.


(afree) #4

Here you go @KunniCan :

There is a better way compared to the Environment.OSVersion suggested above, which is using WMI. unfortunately WMI is not faster but it allows you to get the OS full “friendly” name. here is a sample:

using System.Management;
 
public static string GetMyOSName() {
    string os_name = string.Empty;
    
    // A bit of Linq, you can translate it to a more understandable code.
    os_name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).FirstOrDefault();
    return os_name != null ? os_name+"" : "x";
}

i believe that solution should work no matter what Windows version you are using, and it’s a perfect workaround for the limitation @Yassine mentioned on his post.


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