How can I detect Windows 8.1 in a Desktop application

后端 未结 4 951
滥情空心
滥情空心 2020-12-03 08:36

In the Windows 8.1 preview Environment.OSVersion.Version returns the same version numbers as Windows 8. Is there alternative way of detecting Windows 8.1.

相关标签:
4条回答
  • 2020-12-03 08:52

    Take a look at this article:

    Operating system version changes in Windows 8.1 Preview

    GetVersion(Ex) APIs have been deprecated. That means that while you can still call the APIs, if your app does not specifically target Windows 8.1 Preview, you will get Windows 8 versioning (6.2.0.0).

    What it says is that GetVersion lies to you about the real OS version unless you explicitly direct 8.1 in your manifest.

    You need to add the following to the app manifest:

     <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
        <application> 
          * <!-- Windows 8.1 -->
          * <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
            <!-- Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
        </application> 
    </compatibility>
    

    If you don't want to do that you can check the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

    Current version will give you 6.3

    Current build nmber will be 9431

    0 讨论(0)
  • 2020-12-03 08:56

    Use ntdll!RtlGetVersion. This is what both GetVersionEx and VerifyVersionInfo use, and it gives the correct version number. It takes a pointer to an OSVersionInfoExW structure, like GetVersionExW does. If it succeeds, it returns STATUS_SUCCESS (0).

    0 讨论(0)
  • 2020-12-03 08:58

    Another possibility is to use the VerifyVersionInfo function, which returns the correct result even if your application doesn't have the corresponding manifest file mentioned by user844541.

    Example:

    BOOL CompareWindowsVersion(DWORD dwMajorVersion, DWORD dwMinorVersion)
    {
        OSVERSIONINFOEX ver;
        DWORDLONG dwlConditionMask = 0;
    
        ZeroMemory(&ver, sizeof(OSVERSIONINFOEX));
        ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
        ver.dwMajorVersion = dwMajorVersion;
        ver.dwMinorVersion = dwMinorVersion;
    
        VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
        VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
    
        return VerifyVersionInfo(&ver, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask);
    }
    

    Usage:

    if(CompareWindowsVersion(6, 3))
    {
        // Windows 8.1
    }
    
    0 讨论(0)
  • 2020-12-03 09:03

    I'm not sure that you will want to go this deep but it is easily possibly to get exact operating system version through a simple WMI query call as described below. I've mentioned a method which you can plug directly in your code to get the exact operating system version. Required namespaces to be imported for this C# code snippet have been mentioned just above the function :

    using System;
    using System.Management;
    private string GetOsVersion()
    {
            var sccmManagementScope = new ManagementScope(@"\\" + System.Environment.MachineName + @"\root\cimv2");
            var searchResult = new ManagementObjectSearcher(sccmManagementScope, new WqlObjectQuery("SELECT Version FROM Win32_OperatingSystem"));
            var resultSet = searchResult.Get();
            var osVersion = string.Empty;
            foreach (ManagementObject managementObject in resultSet)
            {
                osVersion = Convert.ToString(managementObject["Version"]);
            }
            return osVersion;
        }
    

    Though I still strongly believe that System.Environment.OSVersion.Version should be able to meet most of your needs unless you have something very specific in regards to target Windows 8.1. In fact I used the same trick for one of requirements as System.Environment class actually lies about the OS version if your application is not manifested for Windows 8.1 operating system.

    0 讨论(0)
提交回复
热议问题