Hossein Moradinia Hossein Moradinia - 1 month ago 18
C# Question

Detect Windows version in .net

How can I detect the Windows OS versions in .net?

What code can I use?

Answer

System.Environment.OSVersion has the information you need for distinguishing most Windows OS major releases, but not all. It consists of three components which map to the following Windows versions:

+--------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|           |   Windows    |   Windows    |   Windows    |Windows NT| Windows | Windows | Windows | Windows | Windows | Windows | Windows | Windows |  Windows | Windows |
|           |     95       |      98      |     Me       |    4.0   |  2000   |   XP    |  2003   |  Vista  |  2008   |    7    | 2008 R2 |    8    |   8.1    |   10    |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|PlatformID | Win32Windows | Win32Windows | Win32Windows | Win32NT  | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT |  ??      |  ??     |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|Major      |              |              |              |          |         |         |         |         |         |         |         |         |          |         |
| version   |      4       |      4       |      4       |    4     |    5    |    5    |    5    |    6    |    6    |    6    |    6    |    6    |     6    |   10    |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|Minor      |              |              |              |          |         |         |         |         |         |         |         |         |          |         |
| version   |      0       |     10       |     90       |    0     |    0    |    1    |    2    |    0    |    0    |    1    |    1    |    2    |     3    |    0    |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+

For a library that allows you to get a more complete view of the exact release of Windows that the current execution environment is running in, check out this library.

EDIT: updated with more versions from this link

EDIT (2011/03/15): Changed PlatformID to actual enum values rather than int values, changed Win 7 to a 1 minor number thanks to comment pointing it out (and I can now confirm with a Win7 machine), added a link to a library that can help.

EDIT (2011/03/15): Added Windows 8.1 and 10 versions.

Important note: if your executable assembly manifest doesn't explicitly state that your exe assembly is compatible with Windows 8.1 and Windows 10.0, System.Environment.OSVersion will return Windows 8.0 version ?! which is 6.2, instead of 6.3 and 10.0!! Source: here first comment.