简体   繁体   中英

Getting software installation path through winreg

I'm trying to get the installation dir of every application installed in my system, then I will store those dir in notepad or anywhere then access that. so basically I want to build a python app like cortana which open any application installed in my system and open it. so this what I thought of

  1. get path of installation from reg with help of winreg
  2. then store it
  3. then access it with other py program that take input of application name and search in that file copy the whole path of that specific application then send it to a python file which has os.open and application start.

and i will store data in sqllite3 or txt file.

the below code doesnt display anything

import winreg

def app(hive, flag):
    areg=winreg.ConnectRegistry(None,hive)
    akey=winreg.OpenKey(areg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                          0, winreg.KEY_READ | flag)
    subkey=winreg.QueryInfoKey(akey)[0]
    soft_list=[]
    for i in range(subkey):
        soft={}
        try:
            soft['path']=winreg.QueryValueEx(subkey, "InstallSource")[0]
        except:
            soft['path']="null"
        soft_list.append(soft)
    return soft_list

soft_list = app(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY) + app(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY) + app(winreg.HKEY_CURRENT_USER, 0)

for software in soft_list:
    print (software['path'])
print(len(soft_list))

this below code works idk y but it dont display all application

import winreg

def foo(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                          0, winreg.KEY_READ | flag)

    count_subkey = winreg.QueryInfoKey(aKey)[0]

    software_list = []

    for i in range(count_subkey):
        software = {}
        try:
            asubkey_name = winreg.EnumKey(aKey, i)
            asubkey = winreg.OpenKey(aKey, asubkey_name)
            software['name'] = winreg.QueryValueEx(asubkey, "DisplayName")[0]

            try:

                software['i']=winreg.QueryValueEx(asubkey,"InstallSource")[0]
            except EnvironmentError:
                software['i'] = winreg.QueryValueEx(asubkey, "InstallSource")[0]

            software_list.append(software)
        except EnvironmentError:
            continue

    return software_list

software_list = foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY) + foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY) + foo(winreg.HKEY_CURRENT_USER, 0)

for software in software_list:
    print (software['name'], software['i'])
print('Number of installed apps: %s' % len(software_list))

and this code below display all application(352 app) but when i add for path the code dont display all application(205)

import winreg

def foo(hive, flag):
    aReg = winreg.ConnectRegistry(None, hive)
    aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                          0, winreg.KEY_READ | flag)

    count_subkey = winreg.QueryInfoKey(aKey)[0]

    software_list = []

    for i in range(count_subkey):
        software = {}
        try:
            asubkey_name = winreg.EnumKey(aKey, i)
            asubkey = winreg.OpenKey(aKey, asubkey_name)
            software['name'] = winreg.QueryValueEx(asubkey, "DisplayName")[0]



            software_list.append(software)
        except EnvironmentError:
            continue

    return software_list

software_list = foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY) + foo(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY) + foo(winreg.HKEY_CURRENT_USER,0)

for software in software_list:
    print (software['name'])
print('Number of installed apps: %s' % len(software_list))

There's no good answer for your question. You won't find all install paths from registry. Also, InstallSource would not be the installed path, but it's source, and is often missing.

Btw, I wrote a package that does what you coded above, called windows_tools.installed_software

from windows_tools.installed_software import get_installed_software

for software in get_installed_software():
    print(software['name'], software['version'], software['publisher'])

From my experience, there's no good way to list all installed programs under windows. You might need to combine various sources, like WMI, registry and perhaps walking over program files and program files (x86)

One way to get the data via WMI:

from windows_tools.wmi_queries import query_qmi

product = query_wmi('SELECT * FROM Win32_Product', 'cimv2', 'test_query', can_be_skipped=False)
for product in products:
    print(product)

Good luck.

Since anshul raj asked for a way to get all executable files in order to find which programs are installed, here's a solution for that problem. Still, this will only list all executable files in paths, and will produce a lot of results for existing programs that have more than one executable.

from ofunctions.file_utils import get_files_recursive

program_paths = [r'C:\Program Files', r'C:\Program Files (x86)']

executables = []
for program_path in program_paths:
    executables += get_files_recursive(program_path, ext_include_list=['.exe'])

print(executables)

Disclaimer: I'm the author of ofunctions module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM