在 .NET 中检测 Windows 版本 [英] Detect Windows version in .NET
问题描述
如何在 .NET 中检测 Windows 操作系统版本?
How can I detect the Windows OS versions in .NET?
我可以使用什么代码?
推荐答案
System.Environment.OSVersion
包含区分大多数 Windows 操作系统主要版本(但不是全部)所需的信息.它由映射到以下 Windows 版本的三个组件组成:
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:
+------------------------------------------------------------------------------+
| | PlatformID | Major version | Minor version |
+------------------------------------------------------------------------------+
| Windows 95 | Win32Windows | 4 | 0 |
| Windows 98 | Win32Windows | 4 | 10 |
| Windows Me | Win32Windows | 4 | 90 |
| Windows NT 4.0 | Win32NT | 4 | 0 |
| Windows 2000 | Win32NT | 5 | 0 |
| Windows XP | Win32NT | 5 | 1 |
| Windows 2003 | Win32NT | 5 | 2 |
| Windows Vista | Win32NT | 6 | 0 |
| Windows 2008 | Win32NT | 6 | 0 |
| Windows 7 | Win32NT | 6 | 1 |
| Windows 2008 R2 | Win32NT | 6 | 1 |
| Windows 8 | Win32NT | 6 | 2 |
| Windows 8.1 | Win32NT | 6 | 3 |
+------------------------------------------------------------------------------+
| Windows 10 | Win32NT | 10 | 0 |
+------------------------------------------------------------------------------+
如果有一个库,您可以更完整地了解当前运行环境所运行的 Windows 的 exact 版本,请查看 这个库.
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.
重要说明:如果您的可执行程序集清单未明确声明您的 exe 程序集与 Windows 8.1 和 Windows 10.0 兼容,System.Environment.OSVersion
将返回Windows 8 版本,即 6.2,而不是 6.3 和 10.0!来源:此处.
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 version, which is 6.2, instead of 6.3 and 10.0! Source: here.
更新:在 .NET 5.0 及更高版本中,System.Environment.OSVersion
始终返回实际的操作系统版本.有关详细信息,请参阅 Environment.OSVersion 返回正确的操作系统版本.
Update: In .NET 5.0 and later, System.Environment.OSVersion
always returns the actual OS version. For more information, see Environment.OSVersion returns the correct operating system version.
这篇关于在 .NET 中检测 Windows 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!