none
Win 8 result should be Win 10 RRS feed

  • Question

  • For Win 10, I am getting Win 8 as a result from this switch. Please comment.

                switch (vsMajor)
                {
                    case 3:
                        osName =  "Win NT 3.51";
                        break;
                    case 4:
                        osName =  "Win NT 4.0";
                        break;
                    case 5:
                        if (vsMinor == 0)
                            osName = "Win 2000";
                        else
                            osName = "Win XP";
                            break;
                    case 6:
                            if (vsMinor == 0)
                                osName = "Win Vista";
                            else if (vsMinor == 1)
                                osName = "Win 7";
                            else if (vsMinor == 2)
                                osName = "Win 8";
                            break;
                    case 10:
                            osName = "Win 10";
                            break;
                }

    Monday, February 24, 2020 1:29 PM

Answers

  • Windows will also lie when returning version information using API functions unless your application has been properly manifested
    • Proposed as answer by cheong00Editor Tuesday, February 25, 2020 1:19 AM
    • Marked as answer by cheong00Editor Thursday, March 5, 2020 9:02 AM
    Monday, February 24, 2020 3:42 PM

All replies

  • The method used by MS to get OS version (Winver or ver commands)

    is by reading the key :

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

    • Proposed as answer by simonb549 Tuesday, February 25, 2020 7:39 AM
    Monday, February 24, 2020 1:49 PM
  • Your code isn't correct nor maintainable. This has been a problem for well over a decade so MS provided helper methods to get you this information without you trying to guess (wrong, generally) what it is. Use the API, forget your programmatic approach. 

    Notice in the API that it purposefully doesn't try to identify "Windows 7" vs "Windows 10". That has also been a big problem for decades as well. Testing functionality based upon OS name is wrong. If you need a specific feature from an OS then test for the feature. Also some OS names have the same version. For example you are targeting client machines but the server machines have the same versioning #s. Hence using just the version # doesn't actually tell you the OS name. If you really want to get the current OS name then use WMI.

    //From WMI Code Creator
    using System;
    using System.Management;
    using System.Windows.Forms;
    
    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher("root\\CIMV2", 
                        "SELECT * FROM Win32_OperatingSystem"); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_OperatingSystem instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }


    What is your goal in all this? What happens when Win X comes out?


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, February 24, 2020 3:34 PM
    Moderator
  • Windows will also lie when returning version information using API functions unless your application has been properly manifested
    • Proposed as answer by cheong00Editor Tuesday, February 25, 2020 1:19 AM
    • Marked as answer by cheong00Editor Thursday, March 5, 2020 9:02 AM
    Monday, February 24, 2020 3:42 PM
  • The method used by MS to get OS version (Winver or ver commands)

    is by reading the key :

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

    For Win 10, for the time being, I cheated. Using the registry key, I get a string of "6.3". This is what I expected for Win 10 and I hard coded it for now. It is just a little utility for me to tell me what computer I am on.

    It did leave me scratching my head a bit since I forget how to parse the "6.3" into its two parts - the "6" and the "3". Can you suggest a way?

    Also, I am getting the IPv6 address when I expected to get the IPv4 one. I'll google that a bit.

    Monday, February 24, 2020 5:42 PM
  • You can see the functions :

    GetWindowsMajorVersionNumber

    GetWindowsMinorVersionNumber

    from : https://github.com/project-jedi/jcl/pull/18/files#diff-ea48926a7d3868ba7cb1597fc48cff54R3858

    • Proposed as answer by simonb549 Tuesday, February 25, 2020 7:39 AM
    Monday, February 24, 2020 5:54 PM
  • The method used by MS to get OS version (Winver or ver commands)

    is by reading the key :

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

    For Win 10, for the time being, I cheated. Using the registry key, I get a string of "6.3". This is what I expected for Win 10 and I hard coded it for now. It is just a little utility for me to tell me what computer I am on.

    It did leave me scratching my head a bit since I forget how to parse the "6.3" into its two parts - the "6" and the "3". Can you suggest a way?

    Also, I am getting the IPv6 address when I expected to get the IPv4 one. I'll google that a bit.

    Hi, Jack0987.

    The most important thing everybody else has already said is:  Is any part of your code really, actually, totally, completely, absolutely and positively DEPENDENT on a specific OS version or patchlevel?  If not, why on Earth do you care what version is installed when your app runs?  That's really a job for an installer (MSI or EXE bundle).

    Good reasons to test the OS version and/or patchlevel is for instance to see whether you need to use UAC Elevation or not, to see whether you should install your Mirror Driver or set up your newfangled Display Duplication API accessor, or to verify whether you'll have access to WinAPI functions that were discontinued such as with Win9x and Win2k.  Because C# is a DotNET language and your app won't even run without the correct DotNET Framework version installed, you should literally never, ever need to know anything about the OS.

    Apart from that you could just simply String.Split("6.3", '.') and you'd get an array where the major version was in element 0 and the minor in element 1.


    Monday, February 24, 2020 7:49 PM
  • > This has been a problem for well over a decade ...

    Yes indeed, and Microsoft has no one to blame but themselves.  I wrote several strongly-worded articles about this at the time, but they were ignored.  Microsoft started lying about the version numbers in order to mollify stupid programmers who had ignored the rules in the first place.  The more they lied, the more programmers did stupid things.  The CORRECT path would have been for Windows to tell the truth, and let those stupid programmers reap the whirlwind of their stupidity.

    Most of these inquiries are not about version compatibility.  It's because people want to display the CORRECT Windows version on their About Box, and so they can collect the data for telemetry.  Microsoft has made that damned near impossible.  Shame on them.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, February 24, 2020 9:54 PM
  • Unless you included manifest that declare your application is Win10 ready, it will lie to you.

    https://docs.microsoft.com/en-us/windows/win32/sysinfo/targeting-your-application-at-windows-8-1

    And yes, given Microsoft declared Win10 will be the last version of Windows, you would want to look at the features directly instead of version or even build numbers.
    Tuesday, February 25, 2020 1:18 AM
    Answerer
  • For how to probe whether a method/overload exists:

    https://devblogs.microsoft.com/oldnewthing/20200325-00/?p=103588

    Monday, March 30, 2020 5:59 AM
    Answerer