2

我正在编写一个打印出详细 Windows 版本信息的函数,输出可能是这样的元组:

('32bit', 'XP', 'Professional', 'SP3', 'English')

它将支持 Windows XP 及更高版本。而且我坚持使用 Windows 版本,例如“专业版”、“家庭基本版”等。

platform.win32_ver() 或 sys.getwindowsversion() 不适合我。

win32api.GetVersionEx(1)几乎命中,但看起来它没有告诉我足够的信息。

然后我看到了GetProductInfo(),但看起来它没有在 pywin32 中实现。

有什么提示吗?

4

3 回答 3

3

您可以ctypes用来访问任何 WinAPI 函数。GetProductInfo()windll.kernel32.GetProductInfo.

我找到了MSDN "Getting the System Version" 示例的Python 版本(GPL 许可,但您可以在那里查看函数的用法)。

于 2010-05-12T08:06:27.433 回答
2

如果 ctypes 不起作用(由于 32 位还是 64 位?),这个 hack 应该:

def get_Windows_name():
    import subprocess, re
    o = subprocess.Popen('systeminfo', stdout=subprocess.PIPE).communicate()[0]
    try: o = str(o, "latin-1")  # Python 3+
    except: pass  
    return re.search("OS Name:\s*(.*)", o).group(1).strip()

print(get_Windows_name())

或者只是阅读注册表:

try: import winreg
except: import _winreg as winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion") as key:
    print(winreg.QueryValueEx(key, "EditionID")[0])

或者使用这个:

from win32com.client import GetObject
wim = GetObject('winmgmts:')
print([o.Caption for o in wim.ExecQuery("Select * from Win32_OperatingSystem")][0])
于 2011-10-27T10:10:56.513 回答
2

我尝试了上面的一些解决方案,但我一直在寻找能给我“Windows XP”或“Windows 7”的东西。平台中还有更多方法可以公开更多信息。

import platform
print platform.system(),platform.release()
于 2012-07-20T10:45:00.650 回答