简体   繁体   中英

C# Get OS Name Windows 8.1

I like to check if the running window is windows 8 or windows 8.1. With the Windows Major Check

6.2 -> Win 8

6.3 -> Win 8.1

That doesn't work, because with the release of Windows 8.1, the behavior of the GetVersion API has changed in the value it will return for the operating system version.

How can I still get the Version correctly?

You could find that info in the registry of Windows. For example, if you have installed Windows 8.1 Pro Edition and execute this lines:

 using Microsoft.Win32;     
 //...

 var windowsName= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "ProductName","");
 var version= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "CurrentVersion", "");
 var build= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "CurrentBuild", "");

You will get Windows 8.1 Pro , 6.3 and 9600 respectively.

Also you could use the WMI to get the Windows name, check the answer in this post :

public static string GetOSFriendlyName()
{
  string result = string.Empty;
  ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
  foreach (ManagementObject os in searcher.Get())
  {
    result = os["Caption"].ToString();
    break;
  }
  return result;
}

try this

it concats the major version no followed by the minor version there are many methods to play with according here Environment Class (System)

string ver = Environment.OSVersion.Version.Major + "." + Environment.OSVersion.Version.Minor;

and the output would be:

6.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM