Search code examples
pythonregistrykeywinreg

Python: Look into the registry to find install location of a program


I am trying to find the install location of a programme using the windows registry. I have managed to find the key and the value that I need. They are found in the Software\Microsoft\Windows\CurrentVersion\Uninstall folder. However when I run the following script, it can't find the file.

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)

The Traceback:

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

How is it that I can see the key but can't seem to access it.


Solution

  • You're asking for the InstallLocation value of SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.

    You want the InstallLocation value of some subkey under SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.

    If you want a particular subkey, just add its name to that path.

    If you want all of them, use the EnumKey function. Something like this:

    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)