如何获取Windows版本——比如“Windows 10,版本1607”?

30

看起来,在提到Windows时,“版本”这个词被用于不同的事情。例如,微软将Windows 10“周年更新”标记为“版本1607”(例如此处)。但是,如果我尝试使用以下代码获取“版本”(在安装了周年更新的计算机上),则没有返回类似于“1607”的内容。


// Get Version details
Version ver = os.Version;
Console.WriteLine("Major version: " + ver.Major);
Console.WriteLine("Major Revision: " + ver.MajorRevision);
Console.WriteLine("Minor version: " + ver.Minor);
Console.WriteLine("Minor Revision: " + ver.MinorRevision);
Console.WriteLine("Build: " + ver.Build);

我收到了这个:

Major version: 6
Major Revision: 0
Minor version: 2
Minor Revision: 0
Build: 9200

如何获取Windows 10的“版本”,比如“版本1607”?

谢谢!


如果您想让Environment.OSVersion反映正确的Windows Build版本,请参阅此处有关创建应用程序清单文件的答案:https://dev59.com/L5zha4cB1Zd3GeqPHptf。与年月样式版本号相比,这可能更有用,因为用户正在获取Insider更新。 - foson
4个回答

42
根据 MSDN 的官方链接,每个 Windows 版本都有一个特定的版本号。在 .NET 中,可以使用 Environment.OSVersion 对象读取该版本号。

根据 MSDN 官方链接,每个 Windows 版本都有一个特定的版本号。在 .NET 中,可以使用 Environment.OSVersion 对象读取该版本号。

Console.WriteLine("OSVersion: {0}", Environment.OSVersion);
//output: OSVersion: Microsoft Windows NT 6.2.9200.0
你要找的是叫做ReleaseID而不是Windows版本号。可以从注册表键中读取:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ReleaseId。
using Microsoft.Win32;

string releaseId = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
Console.WriteLine(releaseId);

3
很棒!已经验证在我的PC上使用“Windows 10版本1511”和另一台PC上使用“版本1607”是可行的——这正是我们期望的。 - Joe Gayetty
3
@Stavm 提供了很棒的信息。顺便说一下,微软确实将“ReleaseId”(例如“1803”)称为版本,详情请参见《Windows 10 发布信息》(https://www.microsoft.com/en-us/itpro/windows-10/release-information)。 - Ken
此外,Windows 10 的原始(RTM)版本没有这个密钥(就像 Windows 7 或其他 Windows 版本一样)。 - gog
在Win7上,字符串releaseId返回为空。 - Jonney
2
这个已经不能用了。微软破坏了21h1的ReleaseID。您必须使用DisplayVersion作为替代,但这可能在将来也会无法使用,并且对于旧操作系统也不起作用。 - az1d

5
string Version = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion", "ProductName", null);

给出一个类似于“Windows 10 Enterprise”的名称。

1
这个已经在几个月前作为答案发布了 这里 - Wai Ha Lee

3
 private static ManagementObject GetMngObj(string className)
    {
        var wmi = new ManagementClass(className);

        foreach (var o in wmi.GetInstances())
        {
            var mo = (ManagementObject)o;
            if (mo != null) return mo;
        }

        return null;
    }

    public static string GetOsVer()
    {
        try
        {
            ManagementObject mo = GetMngObj("Win32_OperatingSystem");

            if (null == mo)
                return string.Empty;

            return mo["Version"] as string;
        }
        catch (Exception e)
        {
            return string.Empty;
        }
    }

如何使用:
Console.WriteLine(GetOsVer());

结果: 10.0.0.1299


2
除了Scott的答案之外,您还可以使用以下代码获取产品名称(例如Windows 10 Pro):(*我不会因为Scott提到注册表路径而得到任何信用,下面的代码是我重复使用的):
```powershell (Get-WmiObject -Class Win32_OperatingSystem).Caption ```
此命令将返回操作系统的名称和版本号。
using Microsoft.Win32;

string ProductName = 
Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "").ToString();
Console.WriteLine(ProductName);

处理32/64位问题;在64位操作系统中运行的32位应用程序可能会检索到错误的值。请查看此线程stackLink - antonio

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