How can I detect Windows 8.1 in a Desktop application

后端 未结 4 978
滥情空心
滥情空心 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 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.

提交回复
热议问题