【即是过客】日常分享002 之 自动获取程序安装路径
评论
收藏

【即是过客】日常分享002 之 自动获取程序安装路径

经验分享
即是过客
2025-07-12 08:39·浏览量:1046
即是过客
影刀专家
影刀认证工程师
发布于 2025-05-20 16:13更新于 2025-07-12 08:391046浏览

___ጿ ኈ ቼ ዽ ጿ   功能:自动识别获取程序安装路径   ጿ ኈ ቼ ዽ ጿ___


情景:

每个用户每台电脑的程序可能会安装到不同盘符、不同路径,导致无法精确启动指定应用,例如A用户可能安装在C盘AAA目录,B用户可能安装在D盘BBB目录,那么在执行操作运行应用时,就很不方便

如果你选择手动操作,那当我没说
    困

但手动操作存在以下痛点:

• 路径分散:不同应用的安装路径各不相同,可能散落在系统盘、用户目录甚至自定义路径中。
• 效率低下:逐个打开控制面板或资源管理器查找手动填写路径,耗时且容易出错。
• 依赖人工记忆:若路径中包含特殊符号或长文件名,手动记录容易遗漏或拼写错误。

使用截图:

  • 精确匹配

获取影刀安装路径

  • 模糊匹配

获取影刀安装路径



Python代码:

请确保安装必要的Python库 pywin32


import os
import re
import winreg
import win32com.client
from typing import *

try:
    from xbot.app.logging import trace as print
except:
    from xbot import print

def main(program_name, exact_match=True):
    
    # 存储找到的路径
    found_paths = []
    
    # 1. 先在常见安装目录中查找
    def _search_in_common_dirs():
        common_dirs = [
            os.environ.get('ProgramFiles', 'C:\\Program Files'),
            os.environ.get('ProgramFiles(x86)', 'C:\\Program Files (x86)'),
            os.environ.get('LOCALAPPDATA', '') + '\\Programs',
            os.environ.get('APPDATA', '') + '\\Programs'
        ]
        for dir_path in common_dirs:
            if not os.path.exists(dir_path):
                continue
            for root, dirs, files in os.walk(dir_path):
                for file in files:
                    if file.lower().endswith('.exe'):
                        full_path = os.path.join(root, file)
                        if exact_match:
                            if file.lower() == program_name.lower() + '.exe':
                                found_paths.append(full_path)
                        else:
                            if file.lower() == program_name.lower() + '.exe' or program_name.lower() in file.lower() or program_name.lower() in root.lower():
                                found_paths.append(full_path)

    # 2. 从注册表中查找
    def _search_in_registry():
        registry_paths = [
            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths",
            "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths"
        ]
        for reg_path in registry_paths:
            try:
                reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path)
                for i in range(winreg.QueryInfoKey(reg_key)[0]):
                    subkey_name = winreg.EnumKey(reg_key, i)
                    if subkey_name.lower().endswith('.exe'):
                        subkey = winreg.OpenKey(reg_key, subkey_name)
                        path, _ = winreg.QueryValueEx(subkey, "")
                        if exact_match:
                            if subkey_name.lower() == program_name.lower() + '.exe' and os.path.exists(path):
                                found_paths.append(path)
                        else:
                            if subkey_name.lower() == program_name.lower() + '.exe' or program_name.lower() in subkey_name.lower() or program_name.lower() in path.lower():
                                found_paths.append(path)

            except WindowsError:
                continue

    # 3. 查找快捷方式
    def _search_shortcuts():
        # 获取开始菜单和桌面路径
        start_menu_paths = [
            os.path.join(os.environ.get('APPDATA', ''), 'Microsoft\\Windows\\Start Menu\\Programs'),
            os.path.join(os.environ.get('ProgramData', ''), 'Microsoft\\Windows\\Start Menu\\Programs')
        ]
        desktop_paths = [
            os.path.join(os.environ.get('USERPROFILE', ''), 'Desktop'),
            os.path.join(os.environ.get('PUBLIC', ''), 'Desktop')
        ]
        shortcuts_paths = start_menu_paths + desktop_paths
        shell = win32com.client.Dispatch("WScript.Shell")
        for shortcut_dir in shortcuts_paths:
            if not os.path.exists(shortcut_dir):
                continue
            for root, dirs, files in os.walk(shortcut_dir):
                for file in files:
                    if file.endswith('.lnk'):
                        shortcut_name = os.path.splitext(file)[0]
                        full_path = os.path.join(root, file)
                        if exact_match:
                            if shortcut_name.lower() == program_name.lower():
                                try:
                                    shortcut = shell.CreateShortCut(full_path)
                                    target_path = shortcut.Targetpath
                                    if os.path.exists(target_path) and target_path.endswith('.exe'):
                                        found_paths.append(target_path)
                                except Exception:
                                    continue
                        else:
                            if shortcut_name.lower() == program_name.lower() or program_name.lower() in shortcut_name.lower():
                                try:
                                    shortcut = shell.CreateShortCut(full_path)
                                    target_path = shortcut.Targetpath
                                    if os.path.exists(target_path) and target_path.endswith('.exe'):
                                        found_paths.append(target_path)
                                except Exception:
                                    continue

    # 执行查找
    _search_in_common_dirs()
    _search_in_registry()
    _search_shortcuts()
    # 返回结果
    if found_paths:
        # 去重
        found_paths = list(set(found_paths))
        return found_paths  # 返回找到的路径
    else:
        return f"未找到程序 '{program_name}' 的安装路径"


收藏15
全部评论1
最新
发布评论
评论