当前位置:首页 >> 脚本专栏

python实现查找所有程序的安装信息

如题,答案是:通过注册表查找。

windows系统中有两支子健存储有所有程序的安装信息:

1,HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store

python实现查找所有程序的安装信息

以下是查找360浏览器安装路径的python代码示例:

import win32con, win32api

def find_360se_path(): 
 upper_keyword = '360SE.EXE' 
 path = None 
 sub_key = r'Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store' 
 key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, sub_key, 0, win32con.KEY_READ) 
 info = win32api.RegQueryInfoKey(key) 
 for i in range(0, info[1]):  
  value = win32api.RegEnumValue(key, i)  
  if value[0].upper().endswith(upper_keyword):
   path = value[0]   
   break 
 win32api.RegCloseKey(key) 
 return path

2,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

python实现查找所有程序的安装信息

以下是查找360浏览器安装路径的python代码示例:

import win32con, win32apidef 

find_360se_path(): 
 upper_value_keyword = '360SE.EXE' 
 upper_item_name_keyword = '360SE6.EXE' 
 path = None 
 sub_key1 = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths' 
 key1 = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, sub_key1, 0, win32con.KEY_READ) 
 info1 = win32api.RegQueryInfoKey(key1) 
 for i in range(0, info1[0]):  
  key_name = win32api.RegEnumKey(key1, i)  
  if key_name.upper() == upper_item_name_keyword:
   sub_key2 = sub_key1 + '\\' + key_name
   key2 = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, sub_key2, 0, win32con.KEY_READ)   
   info2 = win32api.RegQueryInfoKey(key2)   
   for j in range(0, info2[1]):    
    key_value = win32api.RegEnumValue(key2, j)[1]
    if key_value.upper().endswith(upper_value_keyword): 
     path = key_value     
     break   
   win32api.RegCloseKey(key2)   
  break 
 win32api.RegCloseKey(key1) 
 return path