2014-01-13 74 views
1

我正在尝试使用Windows注册表来查找程序的安装位置。我设法找到了我需要的关键和价值。它们位于Software \ Microsoft \ Windows \ CurrentVersion \ Uninstall文件夹中。但是,当我运行以下脚本时,它无法找到该文件。Python:查找注册表以查找程序的安装位置

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 

它是如何,我可以看到的关键,但似乎无法对其进行访问。

回答

1

你在询问InstallLocation的值SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

你想要InstallLocationSOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下的一些子项。

如果您想要一个特定的子项,只需将其名称添加到该路径。如果你想全部使用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) 
+0

如果我指定的aKey =打开项确切的程序(AREG,r'SOFTWARE \微软\的Windows \ CurrentVersion \卸载\安道尔SDK_is1' ,0,KEY_READ)。它只会找到这个程序的安装位置。我已经尝试过了,它会像上面那样返回回溯。 – Marmstrong

+0

@Marmstrong:你确定你的名字完全正确吗?你有没有尝试过使用'EnumKey'来查看所有的键,看看它是否显示? – abarnert