You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python从Windows开始菜单获取可执行文件路径

从Windows开始菜单获取程序可执行文件路径的Python实现

核心原理

Windows开始菜单的所有程序入口本质都是.lnk格式的快捷方式文件,存储在两个固定目录下,解析这些快捷方式即可拿到对应的可执行文件路径。

前置准备

首先安装Windows API依赖库:

pip install pywin32

完整实现步骤

  1. 读取两个开始菜单目录的所有快捷方式:
  • 当前用户专属开始菜单路径:%APPDATA%\Microsoft\Windows\Start Menu\Programs
  • 全局所有用户共用开始菜单路径:C:\ProgramData\Microsoft\Windows\Start Menu\Programs
  1. 解析.lnk文件的目标路径
    下面是可直接使用的示例代码:
import os
import win32com.client

def get_exe_path_from_startmenu(program_name):
    # 初始化快捷方式解析对象
    shell = win32com.client.Dispatch("WScript.Shell")
    # 开始菜单的两个存储路径
    startmenu_paths = [
        os.path.join(os.getenv("APPDATA"), r"Microsoft\Windows\Start Menu\Programs"),
        r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs"
    ]
    exe_paths = []
    for base_path in startmenu_paths:
        # 遍历目录下所有lnk文件
        for root, dirs, files in os.walk(base_path):
            for file in files:
                if file.lower().endswith(".lnk") and program_name.lower() in file.lower():
                    # 过滤卸载相关的快捷方式
                    if "uninstall" in file.lower() or "卸载" in file.lower():
                        continue
                    lnk_path = os.path.join(root, file)
                    shortcut = shell.CreateShortCut(lnk_path)
                    target_path = shortcut.Targetpath
                    if target_path.lower().endswith(".exe") and target_path not in exe_paths:
                        exe_paths.append(target_path)
    return exe_paths

# 示例:获取Chrome浏览器的路径
if __name__ == "__main__":
    chrome_paths = get_exe_path_from_startmenu("chrome")
    print(chrome_paths)

可选优化项

  • 匹配时支持模糊匹配,也可以根据需要调整为精准匹配快捷方式名称
  • 返回结果可以优先返回在Program Files或者Program Files (x86)目录下的路径,优先级高于用户目录下的路径
  • 解析到目标路径后可以先判断文件是否真实存在,避免返回无效路径

内容的提问来源于stack exchange,提问作者Gabriel Tkacz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 21:36:04