从注册表中检查Windows版本(C#)

4

我有一个问题,如何在C#中通过注册表检查Windows版本?

(从Windows XP到Windows 8.1)


5
要求编写代码的问题必须展现出对问题的基本理解。包括尝试过的解决方案,为何不起作用以及预期的结果。 - Federico Berasategui
可能是如何获取“友好”的操作系统版本名称?的重复问题。 - Oliver Gray
1
我的问题是,为什么你要检查注册表来获取版本信息?难道你不能从Environment类中获取吗? - Sriram Sakthivel
3个回答

7

Environment.OSVersion可以帮助您获取此信息!

阅读Environment类的MSDN文档,以查看您可以从该类中获取的所有其他信息。


6

Environment.OSVersion就像其他人已经说明的那样是正确的方法。

但是,如果有人想通过注册表获取它,可以使用以下方法 -

using (Microsoft.Win32.RegistryKey key = 
       Microsoft.Win32.Registry.LocalMachine
               .OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion"))
{
    var osVersion = key.GetValue("CurrentVersion");
}

版本注册表为 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

另外,与实际操作系统相对应的映射可从此处获得 -

Operating system        Version number
-----------------       --------------
Windows 8                   6.2
Windows Server 2012         6.2
Windows 7                   6.1
Windows Server 2008 R2      6.1
Windows Server 2008         6.0
Windows Vista               6.0
Windows Server 2003 R2      5.2
Windows Server 2003         5.2
Windows XP 64-Bit Edition   5.2
Windows XP                  5.1
Windows 2000                5.0

请注意,我已经看到过这种情况...WindowsNT 实际上可能有一个空格...因此键可以是 "Windows NT"。 - Alexandru
如何将 osVersion 输出到 MessageBox?! - Black
我正在使用Windows 10并获取了6.2 - Black
@Black - 对于Windows 10,您还需要检查CurrentMajorVersionNumber和CurrentMinorVersionNumber。有关更多详细信息,请参阅此链接 - https://dev59.com/_V0Z5IYBdhLWcg3wxCuQ#31143093 - Rohit Vats
System.Environment.OSVersion 会返回兼容模式而不是实际的操作系统版本,除非你正在使用 .Net Framework 4.7+。 - undefined

1

在Win8.1的情况下,版本号为6.3.*。 - AMIT SHELKE
System.Environment.OSVersion会返回兼容模式而不是实际的操作系统版本,除非您使用的是.Net Framework 4.7+。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接