简体   繁体   English

在不赞成使用GetVersion / GetVersionEx的情况下,如何在Win8.1中获取操作系统版本?

[英]How to get the OS version in Win8.1 as GetVersion/GetVersionEx are deprecated?

I have scenarios where i want to specifically know the OS major/minor version and build number etc. 我在某些情况下想特别了解OS主要/次要版本和内部版本号等。

From windows 8.1 onwards GetVersion and GetVersionEx have been deprecated, stating: 从Windows 8.1开始,已弃用GetVersionGetVersionEx ,声明:

[ GetVersion / GetVersionEx may be altered or unavailable for releases after Windows 8.1. [ GetVersion / GetVersionEx可能已更改或在Windows 8.1之后的版本中不可用。 Instead, use the Version Helper functions] 而是使用版本帮助器功能]

None of the version helper APIs help me get the OS version number rather help me verify or get to know if my version is same or above some mentioned version. 没有一个版本帮助器API可以帮助我获取操作系统版本号,而不能帮助我验证或了解我的版本是否相同或高于某些所述版本。 What can be done? 该怎么办?

The API GetVersionEx() continues to work in Windows 8.1+, but Microsoft has altered its functionality. API GetVersionEx()继续在Windows 8.1+中运行,但是Microsoft更改了其功能。 From MSDN (emphasis mine): 从MSDN(重点是我的):

With the release of Windows 8.1, the behavior of the GetVersionEx API has changed in the value it will return for the operating system version. 在Windows 8.1发行版中,GetVersionEx API的行为已更改,它将针对操作系统版本返回。 The value returned by the GetVersionEx function now depends on how the application is manifested . GetVersionEx函数返回的值现在取决于应用程序的显示方式

Applications not manifested for Windows 8.1 will return the Windows 8 OS version value (6.2). 未针对Windows 8.1列出的应用程序将返回Windows 8 OS版本值(6.2)。 Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. 在针对给定的操作系统版本显示了应用程序后,GetVersionEx将始终返回在将来的发行版中针对该应用程序显示的版本。 To manifest your applications for Windows 8.1 please refer to Targeting your application for Windows 8.1. 要显示适用于Windows 8.1的应用程序,请参阅面向Windows 8.1的应用程序。

What you need to do is add the proper GUID(s) to your application (.exe/.dll) binaries (via manifest XML information). 您需要做的是(通过清单XML信息)将适当的GUID添加到应用程序(.exe / .dll)二进制文件中。 In other words, if you specifically state your application supports 8.1, GetVersionEx() will return proper information when running on Windows 8.1. 换句话说, 如果您明确声明应用程序支持8.1,则在Windows 8.1上运行时,GetVersionEx()将返回正确的信息 If you do not, GetVersionEx() will lie to you. 如果您不这样做,GetVersionEx()将对您说谎。

See Targeting your application For Windows 8.1 for a list of GUIDs. 有关GUID的列表,请参阅针对Windows 8.1定位应用程序 Also covered here and here . 这里 和这里涵盖

GUID List for the Lazy 懒惰的GUID列表

  • Vista / Server 2008: {e2011457-1546-43c5-a5fe-008deee3d3f0} Vista / Server 2008: {e2011457-1546-43c5-a5fe-008deee3d3f0}
  • Windows 7 / Server 2008 R2: {35138b9a-5d96-4fbd-8e2d-a2440225f93a} Windows 7 / Server 2008 R2: {35138b9a-5d96-4fbd-8e2d-a2440225f93a}
  • Windows 8 / Server 2012: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38} Windows 8 / Server 2012: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}
  • Windows 8.1 / Server 2012 R2 : {1f676c76-80e1-4239-95bb-83d0f6d0da78} Windows 8.1 / Server 2012 R2: {1f676c76-80e1-4239-95bb-83d0f6d0da78}
  • Windows 10 / Server 2016: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a} Windows 10 / Server 2016: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}

As for Windows Server 2019, I'm not sure that a new GUID has been released. 对于Windows Server 2019,我不确定是否已发布新的GUID。 Please comment if you know more! 如果您了解更多,请发表评论!

There is a new function named GetProductInfo that returns version infos. 有一个名为GetProductInfo的新函数,该函数返回版本信息。

If you want to test for a specific version you should use even VerifyVersionInfo 如果要测试特定版本,则甚至应使用VerifyVersionInfo

It is easy to create a structure to check whether a specific OS version is running. 创建结构以检查特定的OS版本是否正在运行很容易。 VerifyVersionInfo takes to version structures and you can easily check for VER_GREATER_EQUAL and VER_LESS_EQUAL VerifyVersionInfo采用版本结构,您可以轻松检查VER_GREATER_EQUAL和VER_LESS_EQUAL

Also note that GetVersionEx doesn't lie on a Windows 8.1 system if you define the correct supported OS entry in the compatibility section for your manifest. 还要注意,如果在清单的兼容性部分中定义了正确的受支持的OS条目,则GetVersionEx不会位于Windows 8.1系统上。 But it may lie in a future OS version! 但这可能在于将来的OS版本中!

See Targeting your application For Windows 8.1 for a list of GUIDs. 有关GUID的列表,请参阅针对Windows 8.1定位应用程序 Also covered here . 也涵盖在这里

GUID list for the application manifest 应用程序清单的GUID列表

  • Vista: {e2011457-1546-43c5-a5fe-008deee3d3f0} Vista: {e2011457-1546-43c5-a5fe-008deee3d3f0}
  • Windows 7: {35138b9a-5d96-4fbd-8e2d-a2440225f93a} Windows 7: {35138b9a-5d96-4fbd-8e2d-a2440225f93a}
  • Windows 8: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38} Windows 8: {4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}
  • Windows 8.1: {1f676c76-80e1-4239-95bb-83d0f6d0da78} Windows 8.1: {1f676c76-80e1-4239-95bb-83d0f6d0da78}
  • Windows 10: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a} Windows 10: {8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}

Check this article on codeproject.com , It's working perfectly for Windows 8 : 在codeproject.com上查看此文章 ,它在Windows 8上运行良好:

1) Download .DLL and add it to your project . 1)下载.DLL并将其添加到您的项目中。

2) Use this code to get Operation System Information : 2)使用此代码获取操作系统信息

StringBuilder sb = new StringBuilder(String.Empty);
sb.AppendLine("Operation System Information");
sb.AppendLine("----------------------------");
sb.AppendLine(String.Format("Name = {0}", OSVersionInfo.Name));
sb.AppendLine(String.Format("Edition = {0}", OSVersionInfo.Edition));
if (OSVersionInfo.ServicePack!=string.Empty)
sb.AppendLine(String.Format("Service Pack = {0}", OSVersionInfo.ServicePack));
else
sb.AppendLine("Service Pack = None");
sb.AppendLine(String.Format("Version = {0}", OSVersionInfo.VersionString));
sb.AppendLine(String.Format("ProcessorBits = {0}", OSVersionInfo.ProcessorBits));
sb.AppendLine(String.Format("OSBits = {0}", OSVersionInfo.OSBits));
sb.AppendLine(String.Format("ProgramBits = {0}", OSVersionInfo.ProgramBits));

textBox1.Text = sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM