如何从 Windows 注册表读取 Steam 安装路径?

How to read the Steam Install Path from the Windows Registry?

我正在尝试开发一个简单的 Python 程序来自动检测 steam 安装文件夹的位置。
我知道可以在注册表中按照以下路径找到此信息:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Valve\Steam

例如:
如何从 InstallPath REG_SZ 读取信息以获得:
C:\Program Files (x86)\Steam

如果有人能提供帮助那就太好了

您可以使用 [Python.Docs]: winreg - Windows registry access.

虽然这个问题没有尝试自己解决问题,但还是举个例子:


>>> import sys
>>> import winreg
>>>
>>>
>>> try:
...     hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\WOW6432Node\Valve\Steam")
... except:
...     hkey = None
...     print(sys.exc_info())
...
>>> hkey
<PyHKEY object at 0x00000154FF5D0390>
>>>
>>> try:
...     steam_path = winreg.QueryValueEx(hkey, "InstallPath")
... except:
...     steam_path = None
...     print(sys.exc_info())
...
>>> steam_path
('C:\Program Files (x86)\Steam', 1)
>>> steam_path[0]
'C:\Program Files (x86)\Steam'
>>> steam_path[1] == winreg.REG_SZ
True
>>>
>>> winreg.CloseKey(hkey)

备注:

  • 我必须创建注册表项,因为我没有安装任何 Steam 组件
  • 异常处理仅用于演示目的(考虑到控制台中的事实),您应该详细说明一下
  • 当 运行 来自 032 位 Python 时,您应该 删除 \WOW6432Node 来自键名(和返回的路径会略有不同)