我可以依赖 OSVERSIONINFO.szCSDVersion 或 Environment.OSVersion.ServicePack 始终采用“Service Pack X”形式吗?
标题基本上说明了一切。我需要确定 Windows Service Pack 编号(以数字形式),以及 Environment.OSVersion.ServicePack (基本上只返回 OSVERSIONINFO.szCSDVersion) 仅返回一个字符串。
在我的所有测试中,该字符串的格式为 ""
(无服务包)或 "Service Pack X"
,其中包含 X
是一个数字。所以解析这个的算法应该非常简单。
我的问题:我可以依赖这个字符串来始终具有这种格式吗?
(我的一部分说不,因为它没有记录。另一部分说可以,因为肯定很多现有代码会破坏如果 MS 决定返回 Windows 7 SP2 的“SP 2 (x86)”,那么他们不会这样做。)
The title basically says it all. I need to determine the Windows Service Pack number (in numeric form), and Environment.OSVersion.ServicePack (which basically just returns OSVERSIONINFO.szCSDVersion) just returns a string.
In all my tests, this string turned out to be in the form ""
(no service pack) or "Service Pack X"
, with X
being a number. So the algorithm to parse this should be quite simple.
My question: Can I rely on this string to always have this format?
(One part of me says no, beause it's not documented. The other part says yes, because surely a lot of existing code would break if MS would decide to return, say, "SP 2 (x86)"
for Windows 7 SP2. Thus, they won't do it. Does anyone have more information on that?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能,某些版本使用翻译字符串!如果您查看该链接中图像中的字符串,您会发现只需使用在字符串中找到的第一个数字即可。
OSVERSIONINFOEX是在NT4 SP6中添加的,如果你调用GetVersionEx你只需要在Win9x上处理字符串和<; NT4 SP6 并在其他系统上使用 OSVERSIONINFOEX.wServicePackMajor。
No you can't, some versions use translated strings! If you look at the strings from the image in that link you see that you might get away with just using the first number you find in the string.
OSVERSIONINFOEX was added in NT4 SP6, if you call GetVersionEx you only need to deal with the string on Win9x and < NT4 SP6 and use OSVERSIONINFOEX.wServicePackMajor on other systems.
您应该使用
BuildLabEx
来代替。它具有自 Windows 早期版本以来一直沿用的指定格式。不确定是否可以在 WMI 中找到它(应该可以),但它位于注册表中:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\BuildLabEx
示例:
7601.17640.amd64fre .win7sp1_gdr.110632-1508
如果它让您感觉更舒服,您可以信赖为了简单起见,最初在
CSDVersion
上匹配某个正则表达式,如果不匹配,则故障返回到BuildLabEx
。You should use
BuildLabEx
instead. It has a specified format which has held since early builds of Windows. Not sure if you can find it in WMI (you should be able to), but it's in the registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\BuildLabEx
Example:
7601.17640.amd64fre.win7sp1_gdr.110632-1508
If it makes you feel more comfortable, you could rely initially on
CSDVersion
matching a certain regex for simplicity, and fail back ontoBuildLabEx
if it doesn't match.