-
Notifications
You must be signed in to change notification settings - Fork 2
/
version_query.py
48 lines (42 loc) · 2.02 KB
/
version_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import winreg, os
from difflib import get_close_matches
def get_program_info(program_name):
# Specify the path to the key that contains a list of installed programs
key_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
# Open the key that contains the list of installed programs
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
# Iterate through the subkeys to find the program you want to query
for i in range(winreg.QueryInfoKey(key)[0]):
subkey_name = winreg.EnumKey(key, i)
subkey = winreg.OpenKey(key, subkey_name)
try:
# Check if the program name is in the DisplayName value
display_name = winreg.QueryValueEx(subkey, 'DisplayName')[0]
if program_name in display_name:
# If the program name is in the DisplayName value, get the program key
program_key = subkey_name
break
except OSError:
pass
winreg.CloseKey(subkey)
# Close the installed programs key
winreg.CloseKey(key)
# Check if the program key was found, and return None if not
if not program_key:
return None
else:
# Use the program key to get the version information and installation path
key_path = fr'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{program_key}'
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
version = winreg.QueryValueEx(key, 'DisplayVersion')[0]
install_location = winreg.QueryValueEx(key, 'InstallLocation')[0]
try:
exe_path = winreg.QueryValueEx(key, 'DisplayIcon')[0]
except OSError:
# In my case there was no DisplayIcon key.
file_names = os.listdir(install_location)
exe_file = get_close_matches(program_name, file_names, 1, 0.5)[0]
if exe_file and ".exe" in exe_file:
exe_path = os.path.join(install_location, exe_file)
winreg.CloseKey(key)
return version, install_location, exe_path