12

When call Windows 10 version with:

Environment.OSVersion.ToString()

Return this

enter image description here

Windows 8 and 8.1 version return 6.2 not 6.3 ?!

Im using Windows 10 RTM (upgrade from Insider with windows update) VS 2015 RC and .Net 4.6

Now i need to get the correct version of windows, any solution?

Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
user3477026
  • 241
  • 1
  • 3
  • 15
  • 4
    Be careful when calling things bugs and going all-caps with it. Chances are that the proven framework that you are using is, in fact, not buggy. – zneak Jul 20 '15 at 15:18

3 Answers3

14

It's not a bug, it's in MSDN:

Operating System Version

Windows 10 Insider Preview    10.0*
Windows Server Technical Preview    10.0*
Windows 8.1 6.3*

*: For applications that have been manifested for Windows 8.1 or Windows 10 Insider Preview. Applications not manifested for Windows 8.1 or Windows 10 Insider Preview will return the Windows 8 OS version value (6.2). To manifest your applications for Windows 8.1 or Windows 10 Insider Preview, refer to Targeting your application for Windows.

What do you need the Windows version for anyway?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Environment.OSVersion.ToString() return 6.2 on Windows 8.1 Update 3 not 6.3!! – user3477026 Jul 20 '15 at 15:20
  • 2
    The fact that an application gives you "6.2" on Windows 8.1 means that app is not manifested for 8.1. Read the MSDN page, especially the [Targeting your application for Windows](https://msdn.microsoft.com/en-us/library/windows/desktop/dn481241(v=vs.85).aspx) link. – CodeCaster Jul 20 '15 at 15:23
  • No, you need to add it. – CodeCaster Jul 20 '15 at 15:26
  • It's probably not "perfect" since the OS will continue to tell you that you're running on whatever version of the OS you were compiled for. What do you want to know the OS version for? – Peter Torr - MSFT Jul 22 '15 at 04:11
  • We need it for driver reasons, wich are different for Windows 10 and prio. Any workaround? – Frederiek Aug 06 '15 at 12:14
  • According to **Targeting your application for Windows** link, it is mentioned to add SXS_MANIFEST_RESOURCE_ID, SXS_MANIFEST etc to the sources. Could someone tell where does this needed to be added exactly? – DeeJay007 Oct 24 '17 at 13:19
  • @CodeCaster I typically need to check the OSVersion to write code that is doing something helpful based on available features... like turn on the Receive Side Scaling selectively based on OS version (its off by default in the old OS, on in the new one..., doesn't exist in the even older one.) There may be other ways to detect feature support, but this is the most obvious one. Sure surprises me that the API is deprecated! – Tim Lovell-Smith May 14 '19 at 17:01
14

Use WMI query instead, it is the most reliable way to get the version and related product name.

        public static KeyValuePair<string, string> GetOSVersionAndCaption()
        {
              KeyValuePair<string, string> kvpOSSpecs = new KeyValuePair<string, string>();
              ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption, Version FROM Win32_OperatingSystem");
        try
        {

            foreach (var os in searcher.Get())
            {
                var version = os["Version"].ToString();
                var productName = os["Caption"].ToString();
                kvpOSSpecs = new KeyValuePair<string, string>(productName, version);
            }
        }
        catch { }

        return kvpOSSpecs;
    }
bgcode
  • 326
  • 3
  • 12
  • 1
    You will also want to query for ProductType to distinguish Windows 10 vs Windows Server 2016. ProductType will be one of: 1: Workstation -- Windows 10 2: Domain Controller 3: Server -- Windows Server 2016 – Andrew Dennison May 31 '17 at 16:16
1

Windows 10 has a new registry key - you will get a result of 10 from Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", Nothing) and (in theory) not from earlier versions.

This runs in 0 milliseconds according to the stopwatch object, whereas the WMI method takes at least 30 ms for me.

AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
  • 4
    _"This runs in less than 0 milliseconds"_ - so you're saying that registry key holds the secret to time travel? – CodeCaster Apr 11 '16 at 09:09
  • Indeed, it always amuses me when the time taken is reported as 0ms :) – AjV Jsy Apr 11 '16 at 10:08
  • 2
    30 ms doesn't seem like too high of a price to pay for an API that you only need to call once and actually works correctly for most versions (starting with Windows 2000) prior to Windows 10. Using the registry requires handling the case of earlier versions which do not support CurrentMajorVersionNumber. If you do you use the registry, be aware that Windows Server 2016 also reports CurrentMajorVersionNumber=10. Accessing InstallationType is needed to distinguish them. It can be: Client | Server | ... So you may need 2-3 registry accesses. This may take longer than 3 X 0ms :) – Andrew Dennison May 31 '17 at 16:31