我使用寄存器中的搜索和Win32_Product类来获取计算机上安装的程序的列表,但是它没有提供所有程序,我已经看到C ++程序给出的结果相同就像控制面板中的程序和组件一样。是否有适用于python的api可以给我相同的结果。 这是c ++ https://www.codeproject.com/Articles/6791/How-to-get-a-list-of-installed-applications的代码 那就是我用的: 导入win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer, "root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Product")
for objItem in colItems:
print("Name: ", objItem.Name)
还有注册表:
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
for i in range(1024):
try:
asubkey_name = EnumKey(aKey, i)
asubkey = OpenKey(aKey, asubkey_name)
val = str(QueryValueEx(asubkey, "DisplayName"))
b = "!@#$,01'"
for char in b:
val = val.replace(char, "")
r = len(val)
val = str(val[1:r - 2])
val2 = str(QueryValueEx(asubkey, "DisplayIcon"))
if s.lower() in val.lower():
r = len(val2)
val2 = str(val2[2:r - 5])
# print(val2)
subprocess.Popen(val2)
break
# print(val, val2)
except EnvironmentError:
continue
答案 0 :(得分:1)
使用KEY_WOW64_32KEY
和KEY_WOW64_64KEY
检查32位和64位注册表。此外,一些安装程序将使用HKEY_CURRENT_USER
,尽管很少使用后者。
请注意,pywin32的QueryValueEx
返回一个元组,该元组中的第一个元素包含所需的字符串。 QueryInfoKey
返回一个元组,其第一个元素是子键的总数。
def foo(hive, flag):
aReg = winreg.ConnectRegistry(None, hive)
aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
0, win32con.KEY_READ | flag)
count_subkey = winreg.QueryInfoKey(aKey)[0]
for i in range(count_subkey):
try:
asubkey_name = winreg.EnumKey(aKey, i)
asubkey = winreg.OpenKey(aKey, asubkey_name)
val = winreg.QueryValueEx(asubkey, "DisplayName")[0]
print(val)
except EnvironmentError:
continue
foo(win32con.HKEY_LOCAL_MACHINE, win32con.KEY_WOW64_32KEY)
foo(win32con.HKEY_LOCAL_MACHINE, win32con.KEY_WOW64_64KEY)
foo(win32con.HKEY_CURRENT_USER, 0)
答案 1 :(得分:1)
稍有改进的版本,无需Win32con导入即可工作,并且可以检索软件版本和发布者。感谢Barmak Shemirani的初步回答:)
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['version'] = winreg.QueryValueEx(asubkey, "DisplayVersion")[0]
except EnvironmentError:
software['version'] = 'undefined'
try:
software['publisher'] = winreg.QueryValueEx(asubkey, "Publisher")[0]
except EnvironmentError:
software['publisher'] = 'undefined'
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('Name=%s, Version=%s, Publisher=%s' % (software['name'], software['version'], software['publisher']))
print('Number of installed apps: %s' % len(software_list))