How can I get a real OS version in C #?

Starting with the .NET 4 and Windows 8 or higher platforms, it’s quite difficult to get the version of the OS where your program is running.

For example, working in Windows 10, if my project has a Windows 8 DLL, it Environment.OSVersion.Versionreturns 6.2.9200.0which is Windows 8, not Windows 10.

This question explains this ( crono ): Windows version in C #

So, my question is this: how can we accurately determine (and stay in the .NET way to use it on platforms other than Windows), in the OS version where our application works?

MSDN Link:

https://msdn.microsoft.com/en-us/library/system.environment.osversion(v=vs.110).aspx

+7
source share
1 answer

If using platforms other than Windows is not a problem, you can use Win32_OperatingSystemthe WMI class.

Win32_OperatingSystem represents a Windows based operating system installed on a computer

//using System.Linq;
//using System.Management;
var query = "SELECT * FROM Win32_OperatingSystem";
var searcher = new ManagementObjectSearcher(query);
var info = searcher.Get().Cast<ManagementObject>().FirstOrDefault();
var caption = info.Properties["Caption"].Value.ToString();
var version = info.Properties["Version"].Value.ToString();
var spMajorVersion = info.Properties["ServicePackMajorVersion"].Value.ToString();
var spMinorVersion = info.Properties["ServicePackMinorVersion"].Value.ToString();

Do not forget to add a link to System.Management.dll.

Note:

  • If you want to use the Windows API functions for this purpose, please note that it GetVersionExmay be changed or not available for releases after Windows 8.1. Use the Version Helper functions instead .
  • Starting with Windows 8, the property Environment.OSVersionreturns the same major and minor version numbers for all Windows platforms. Therefore, we do not recommend retrieving the value of this property to determine the version of the operating system.
  • , Mono FAQ - ?
+10

Source: https://habr.com/ru/post/1606082/


All Articles