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

如何使用Python查找Discord.exe以实现程序自动启动?

Windows下Python稳定查找Discord.exe的实现方案

方案核心逻辑

以下三种方案均不需要硬编码版本号,Discord版本更新后可自动适配最新路径,可根据场景单独使用或组合使用。


方法1:遍历默认安装目录匹配最新版本

Discord默认安装在%LOCALAPPDATA%\Discord路径下,所有历史版本均存储在该目录下以app-开头的版本号命名子文件夹中,直接按版本号排序取最新即可。

import os
from pathlib import Path

def get_discord_path():
    local_appdata = os.getenv('LOCALAPPDATA')
    discord_root = Path(local_appdata) / 'Discord'
    if not discord_root.exists():
        return None
    # 匹配所有版本目录
    version_dirs = [d for d in discord_root.iterdir() if d.is_dir() and d.name.startswith('app-')]
    if not version_dirs:
        return None
    # 按版本号降序排序取最新
    version_dirs.sort(key=lambda x: list(map(int, x.name.lstrip('app-').split('.'))), reverse=True)
    latest_dir = version_dirs[0]
    discord_exe = latest_dir / 'Discord.exe'
    return str(discord_exe) if discord_exe.exists() else None

适用场景:默认路径安装的Discord,无第三方依赖


方法2:读取注册表获取安装路径

Discord安装时会在Windows注册表写入卸载信息,从中可直接读取安装路径,覆盖自定义安装路径的场景。

import os
import winreg

def get_discord_path_from_reg():
    # 匹配当前用户安装的Discord
    reg_path = r'Software\Microsoft\Windows\CurrentVersion\Uninstall\Discord'
    try:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path) as key:
            install_location = winreg.QueryValueEx(key, 'InstallLocation')[0]
            discord_exe = os.path.join(install_location, 'Discord.exe')
            return discord_exe if os.path.exists(discord_exe) else None
    except FileNotFoundError:
        # 匹配全局安装/32位兼容路径的Discord
        try:
            reg_path = r'Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Discord'
            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) as key:
                install_location = winreg.QueryValueEx(key, 'InstallLocation')[0]
                discord_exe = os.path.join(install_location, 'Discord.exe')
                return discord_exe if os.path.exists(discord_exe) else None
        except:
            return None

适用场景:自定义安装路径的Discord,无第三方依赖


方法3:解析开始菜单快捷方式(最稳定)

Discord每次更新都会自动同步更新开始菜单的快捷方式目标路径,直接解析快捷方式即可拿到最新可执行文件路径,完全不受安装位置、版本更新影响。
需先安装依赖:pip install pywin32

import os
import pythoncom
from win32com.client import Dispatch

def get_discord_path_from_shortcut():
    # 遍历当前用户和全局开始菜单目录
    start_menu_paths = [
        os.path.join(os.getenv('APPDATA'), r'Microsoft\Windows\Start Menu\Programs\Discord Inc'),
        os.path.join(os.getenv('PROGRAMDATA'), r'Microsoft\Windows\Start Menu\Programs\Discord Inc')
    ]
    for path in start_menu_paths:
        if not os.path.exists(path):
            continue
        for file in os.listdir(path):
            if file.endswith('.lnk') and 'Discord' in file:
                shortcut_path = os.path.join(path, file)
                # 解析快捷方式目标
                pythoncom.CoInitialize()
                shell = Dispatch("WScript.Shell")
                shortcut = shell.CreateShortCut(shortcut_path)
                target_path = shortcut.Targetpath
                if os.path.exists(target_path) and target_path.endswith('Discord.exe'):
                    return target_path
    return None

适用场景:所有正常安装的Discord,稳定性最高


使用建议

可按优先级组合调用三个方法:先调用快捷方式解析方案,失败则调用注册表方案,最后调用默认目录遍历方案,可覆盖99%以上的使用场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 20:09:03