我正在尝试使用 Windows 注册表查找程序的安装位置。我已经找到了我需要的 key 和值(value)。它们位于 Software\Microsoft\Windows\CurrentVersion\Uninstall 文件夹中。但是,当我运行以下脚本时,它找不到该文件。
from _winreg import *
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 0, KEY_READ)
[Pathname,regtype]=(QueryValueEx(aKey,"InstallLocation"))
print Pathname
CloseKey(aKey)
CloseKey(aReg)
回溯:
Traceback (most recent call last):
File "C:\Users\m.armstrong\Desktop\regression\regpy.py", line 7, in <module [Pathname,regtype]=(QueryValueEx(aKey,"InstallLocation"))
WindowsError: [Error 2] The system cannot find the file specified
为什么我可以看到 key ,但似乎无法访问它。
最佳答案
您要求提供 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
的 InstallLocation
值。
您需要SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
下某个子项的InstallLocation
值。
如果您想要特定的子项,只需将其名称添加到该路径即可。
如果您想要全部,请使用 EnumKey
功能。像这样的事情:
for i in itertools.count():
try:
subname = EnumKey(akey, i)
except WindowsError:
break
subkey = OpenKey(akey, subname, 0, KEY_READ)
pathname, regtype = QueryValueEx(subkey, "InstallLocation")
print subname, pathname
CloseKey(subkey)
关于Python:查看注册表以查找程序的安装位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090325/