8

Since .NET 4 and Windows 8 or greater platforms it's quite uncertain to get the OS version where your program is ran.

For instance, running in a Windows 10, if my project has a Windows 8 DLL then the Environment.OSVersion.Version returns 6.2.9200.0 which is Windows 8 and not Windows 10.

This behaviour is explained in this question (crono's answer): Windows version in c#

So my question is the following: How can we detect for sure (and staying in a .NET way in order to be able to use it on non-Windows platforms) the OS version where our application is running?

Link to MSDN:

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

Matteo Migliore
  • 925
  • 8
  • 22
  • I was going to post an answer about [manifesting your application](https://msdn.microsoft.com/en-us/library/windows/desktop/dn481241%28v=vs.85%29.aspx) for Windows 10 so that `Environment.OSVersion.Version` would return the right value (I recently had to do this to get the software I work on to detect Windows 10), but I'm not sure it would help you if you are on a non-Windows platform. I did a bit of searching and it seems that Mono, for instance, seems to have problems listening to the `app.manifest` file. – Wai Ha Lee Sep 05 '15 at 22:04
  • That's what I've found about the matter. However the one that has made the program needs to recompile it if there is a Windows 10.1 or even 11 in order to his app to detect correctly the version. And, considering the lifecycle of an App it can be wrong to run an old version of your app in such Windows when it was thought for previous versions just because OsVersion returns the last version allowed per the manifest. Not sure that I'm clear ... That decision of Microsoft is quite disturbing ... – Antoine Rodriguez Sep 06 '15 at 16:27

1 Answers1

12

If using other platforms than windows is not a concern, you can use Win32_OperatingSystem 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();

Remember to add a reference to System.Management.dll.

Note:

  • If you want to use Windows API Functions for this purpose, pay attention that GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead, use the Version Helper functions.
  • Starting with Windows 8, the Environment.OSVersion property returns the same major and minor version numbers for all Windows platforms. Therefore, we do not recommend that you retrieve the value of this property to determine the operating system version.
  • You may want to take a look at Mono FAQ - How to detect the execution platform?
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Quite interesting ... however : Is there a method that doesn't rely on windows specific technology ? – Antoine Rodriguez Sep 05 '15 at 18:36
  • Take a look at this Mono FAQ: [How to detect the execution platform?](http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform) – Reza Aghaei Sep 05 '15 at 18:42
  • Thank you : A mix between your solution and this page is what I've been looking for. – Antoine Rodriguez Sep 06 '15 at 16:21
  • Glad to hear that you fixed it :) – Reza Aghaei Sep 06 '15 at 16:26
  • works but why loop over `ManagementObject`? is there a possibility of more than 1? if not why not just `searcher.Get().OfType().FirstOrDefault()` ? – wal Oct 25 '16 at 01:53
  • @wal Thank you for your feedback, I enhanced the answer based on your comment. Probably I was going to write the answer without dependency to `System.Linq`, but it's better now :) – Reza Aghaei Oct 25 '16 at 08:19