雇人做淘宝网站多少钱,家具设计图制作软件,个人网站首页界面,成都设计公司展厅设计一个项目中需要获取本机安装的所有应用程序列表#xff0c;花了一点时间研究了一下#xff0c;分享出来。 主要通过访问注册表和桌面快捷方式来完成这一任务#xff0c;因为注册表中获取到的应用程序列表不完全#xff0c;因此通过桌面快捷方式进行补充。 导入所需模块 
im… 一个项目中需要获取本机安装的所有应用程序列表花了一点时间研究了一下分享出来。 主要通过访问注册表和桌面快捷方式来完成这一任务因为注册表中获取到的应用程序列表不完全因此通过桌面快捷方式进行补充。 导入所需模块 
import winreg
import win32com.client
from pathlib import Pathwinreg用于访问Windows注册表。win32com.client用于处理Windows快捷方式。Path用于处理文件和目录路径。 
通过注册表获取安装的应用程序 
def get_installed_apps_from_registry():通过注册表查询安装的应用apps  {}try:# 定义要查询的注册表路径reg_paths  [rSOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall,rSOFTWARE\\WOW6432NODE\\Microsoft\\Windows\\CurrentVersion\\Uninstall]# 遍历所有注册表路径for reg_path in reg_paths:with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) as key:for i in range(winreg.QueryInfoKey(key)[0]):sub_key_name  winreg.EnumKey(key, i)with winreg.OpenKey(key, sub_key_name) as sub_key:try:app_name  winreg.QueryValueEx(sub_key, DisplayName)[0]# 排除驱动程序if 驱动程序 in app_name:continueapp_path  winreg.QueryValueEx(sub_key, DisplayIcon)[0]# 对 exe 路径进行过滤和清洗if exe in app_path:app_path  app_path.replace(, )# 有一些 exe 文件的路径是以逗号分隔的只取第一个app_path  app_path.split(,)[0]apps[app_name]  {path: app_path}except FileNotFoundError:passexcept Exception as e:print(fError: {e})return apps此函数执行以下操作 
定义注册表中存储已安装应用程序信息的路径。遍历这些路径并获取每个应用程序的名称和可执行文件路径。过滤掉包含“驱动程序”的应用名称清洗路径信息构建应用程序字典。 
获取桌面上的快捷方式 
def get_shortcuts_on_desktop():查询桌面上安装的应用程序快捷方式desktop_path  Path.home() / Desktopshortcuts  {}for item in desktop_path.iterdir():if item.suffix  .lnk:shortcut_name  item.stemshortcuts[shortcut_name]  {path: item}return shortcuts此函数执行以下操作 
获取桌面目录路径。遍历桌面上的所有文件筛选出扩展名为.lnk快捷方式的文件并保存其路径和名称。 
根据快捷方式获取目标路径 
def get_target_path_by_shortcut(shortcut_path):根据快捷方式获取应用程序的可执行文件exe路径:param shortcut_path: 快捷方式路径:return: 可执行文件exe路径shell  win32com.client.Dispatch(WScript.Shell)shortcut  shell.CreateShortcut(shortcut_path)target_path  shortcut.TargetPathreturn target_path此函数执行以下操作 
使用win32com.client模块来解析快捷方式。获取快捷方式所指向的目标路径即应用程序的可执行文件路径。 
合并注册表和桌面快捷方式的信息 
def get_installed_apps_dict():获取本机安装的所有应用程序:return: 安装的应用程序字典installed_apps  get_installed_apps_from_registry()desktop_shortcuts  get_shortcuts_on_desktop()merged_apps  {}# 合并安装的应用程序和桌面快捷方式for app_name, app_info in installed_apps.items():merged_apps[app_name]  {path: app_info[path]}# 添加在桌面快捷方式中存在但未在安装应用程序中列出的软件for app_name, shortcut_info in desktop_shortcuts.items():if app_name not in installed_apps:shortcut_path  str(shortcut_info[path])merged_apps[app_name]  {path: get_target_path_by_shortcut(shortcut_path)}return merged_apps此函数执行以下操作 
获取注册表中安装的应用程序和桌面上的快捷方式。合并这两部分信息确保所有应用程序都包含在结果中。如果一个应用程序在桌面快捷方式中存在但未在注册表中列出则从快捷方式获取其路径。 
主程序入口 
if __name__  __main__:app_list  get_installed_apps_dict()for name, info in app_list.items():print(fApp Name: {name})print(fExecutable Path: {info[path]})print()print(fTotal Apps: {len(app_list)})主程序入口执行以下操作 
获取所有安装的应用程序列表。打印每个应用程序的名称和路径。输出总的应用程序数量。 
通过这些步骤该脚本能够有效地获取并列出Windows系统中安装的所有应用程序。