下面的python代码可以实现桌面快捷方式和注册表中安装的程序进行导出,桌面快捷方式的都能正确导出,没有桌面快捷方式的只能读取注册表导出,应为程序安装对注册表的写入不统一,可能存在导出的文件是卸载.exe的路径,或者是图标.ico路径的情况。
这些代码也是在网上找的,东拼西凑的。
[Python] 纯文本查看 复制代码 import winreg
from win32com.client import Dispatch
from pathlib import Path
import socket
def get_installed_apps_from_registry():
apps = {}
# 定义要查询的注册表路径
root_key = [winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE]
reg_paths = [
r"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
r"SOFTWARE\\WOW6432NODE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
]
n = 0
for i in root_key:
a_reg = winreg.ConnectRegistry(None, i)
for j in reg_paths:
try:
open_key = winreg.OpenKey(a_reg, j)
i = 0
while True:
try:
subkey_name = winreg.EnumKey(open_key, i)
subkey = winreg.OpenKey(open_key, subkey_name)
# 读取DisplayName和DisplayVersion
try:
display_name = winreg.QueryValueEx(subkey, "DisplayName")[0]
display_version = winreg.QueryValueEx(subkey, "DisplayVersion")[0]
l = ['DisplayIcon', 'UninstallString', 'InstallLocation', 'BundleCachePath']
install_location = []
for k in l:
try:
install_location = winreg.QueryValueEx(subkey, k)[0]
install_location = install_location.replace('"', '')
install_location = install_location.split(",")[0]
break
except FileNotFoundError:
install_location = "未知"
pass
if "exe" in install_location:
install_location = install_location.replace('"', '')
# 有一些 exe 文件的路径是以逗号分隔的,只取第一个
install_location = install_location.split(",")[0]
apps[display_name] = {"path": install_location, "version": display_version}
else:
install_location = install_location.replace('"', '')
# 有一些 exe 文件的路径是以逗号分隔的,只取第一个
install_location = install_location.split(",")[0]
apps[display_name] = {"path": install_location, "version": display_version}
# apps[display_name] = {"path": install_location, "version": display_version}
n += 1
# print(n)
# print(f"名称: {display_name}")
# print(f"版本: {display_version}")
# print(f"安装路径: {install_location}")
except FileNotFoundError:
pass # 如果某些键值不存在,则跳过
winreg.CloseKey(subkey)
i += 1
# except OSError:
except Exception as e:
print(f"Error: {e}")
break # 没有更多的子键
winreg.CloseKey(open_key)
# except OSError:
except Exception as e:
print(f"Error: {e}")
break # 没有更多的子键
winreg.CloseKey(a_reg)
# print(apps)
return apps
def get_shortcuts_on_desktop():
"""
查询桌面上安装的应用程序快捷方式
"""
desktop_path = Path.home() / "Desktop"
shortcuts = {}
for item in desktop_path.iterdir():
if item.suffix == ".lnk":
shortcut_name = item.stem
shortcuts[shortcut_name] = {"path": item}
return shortcuts
def get_target_path_version_by_shortcut(shortcut_path):
"""
根据快捷方式获取应用程序的可执行文件(exe)路径
:param shortcut_path: 快捷方式路径
:return: 可执行文件(exe)路径
"""
shell = Dispatch("WScript.Shell")
''' 获取文件版本信息,这个兼容性强 '''
information_parser = Dispatch("Scripting.FileSystemObject")
shortcut = shell.CreateShortcut(shortcut_path)
target_path = shortcut.TargetPath
version = information_parser.GetFileVersion(target_path)
return version, target_path
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"], "version": app_info["version"]}
# 添加在桌面快捷方式中存在但未在安装应用程序中列出的软件
for app_name, shortcut_info in desktop_shortcuts.items():
if app_name not in installed_apps:
shortcut_path = str(shortcut_info["path"])
''' 获取文件版本信息,这个兼容性强 '''
p_v = get_target_path_version_by_shortcut(shortcut_path)
merged_apps[app_name] = {"path": p_v[1], "version": p_v[0]}
return merged_apps
if __name__ == '__main__':
app_list = get_installed_apps_dict()
# 检测主机名,并将主机名作文检测结果的文件名
hostname = socket.gethostname()
file = open(r'%s.txt' % hostname, 'w') # 保存在当前目录,使用新建模式
n = 0
for name, info in app_list.items():
n += 1
file.write(f"序号:{n}\n名称: {name}\n版本: {info['version']}\n路径: {info['path']}\n" + '\n')
print(f"序号: {n}")
print(f"名称: {name}")
print(f"版本: {info['version']}")
print(f"路径: {info['path']}")
print()
print(f"安装应用数量: {len(app_list)}")
file.write(f"安装应用数量: {len(app_list)}")
file.close()
|