2014-01-13 69 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