如何用Python将EXE程序添加至Windows已安装应用列表
实现方法
要把你的EXE添加到Windows已安装应用列表,核心是操作Windows注册表——系统就是靠这里的键值生成应用列表的。下面是几种不用第三方安装工具的实现方式:
一、手动修改注册表(最直接的方式)
Windows会读取特定注册表路径的键值来生成已安装应用列表,你需要根据程序位数写入对应路径:
- 64位程序:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\[你的应用唯一ID] - 32位程序(或64位系统上的32位程序):
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\[你的应用唯一ID]
需要添加的关键键值(以字符串类型为主):
DisplayName:应用显示名称DisplayVersion:应用版本号Publisher:发布者名称InstallLocation:应用安装目录UninstallString:卸载命令(格式如"C:\你的应用路径\uninstall.exe")IconPath(可选):应用图标路径,格式为"C:\你的应用路径\app.exe",0(0是图标索引)
示例注册表文件:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyCustomApp] "DisplayName"="我的自定义应用" "DisplayVersion"="1.0.0" "Publisher"="个人开发者" "InstallLocation"="C:\\Program Files\\MyCustomApp" "UninstallString"="\"C:\\Program Files\\MyCustomApp\\uninstall.exe\"" "IconPath"="\"C:\\Program Files\\MyCustomApp\\MyApp.exe\",0"
注意:修改
HKEY_LOCAL_MACHINE路径需要管理员权限,否则会写入失败。
二、Python脚本实现(简化操作)
用Python内置的winreg模块可以直接操作注册表,无需手动编写reg文件:
import winreg import os def add_to_installed_apps(app_name, version, publisher, install_path, uninstall_path, icon_path=None): # 根据系统位数选择注册表路径 if os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64': reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{}".format(app_name.replace(' ', '')) else: reg_path = r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{}".format(app_name.replace(' ', '')) try: # 创建/打开注册表项 key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, reg_path) # 设置各项键值 winreg.SetValueEx(key, "DisplayName", 0, winreg.REG_SZ, app_name) winreg.SetValueEx(key, "DisplayVersion", 0, winreg.REG_SZ, version) winreg.SetValueEx(key, "Publisher", 0, winreg.REG_SZ, publisher) winreg.SetValueEx(key, "InstallLocation", 0, winreg.REG_SZ, install_path) winreg.SetValueEx(key, "UninstallString", 0, winreg.REG_SZ, f'"{uninstall_path}"') if icon_path: winreg.SetValueEx(key, "IconPath", 0, winreg.REG_SZ, f'"{icon_path}",0') winreg.CloseKey(key) print("已成功添加到已安装应用列表") except PermissionError: print("需要管理员权限,请右键以管理员身份运行脚本") except Exception as e: print(f"操作失败:{str(e)}") # 调用示例 add_to_installed_apps( app_name="我的自定义应用", version="1.0.0", publisher="个人开发者", install_path="C:\\Program Files\\MyCustomApp", uninstall_path="C:\\Program Files\\MyCustomApp\\uninstall.exe", icon_path="C:\\Program Files\\MyCustomApp\\MyApp.exe" )
三、Windows API实现(C/C++开发场景)
如果用C/C++写自定义安装程序,可以直接调用Windows注册表API:
#include <windows.h> #include <string> BOOL AddToInstalledApps(const wchar_t* appId, const wchar_t* displayName, const wchar_t* version, const wchar_t* publisher, const wchar_t* installPath, const wchar_t* uninstallCmd) { HKEY hKey; std::wstring regPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"; regPath += appId; // 尝试创建64位程序的注册表项 LONG result = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regPath.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL); if (result != ERROR_SUCCESS) { // 失败则尝试32位程序路径 regPath = L"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"; regPath += appId; result = RegCreateKeyExW(HKEY_LOCAL_MACHINE, regPath.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL); if (result != ERROR_SUCCESS) { return FALSE; } } // 设置各项键值 RegSetValueExW(hKey, L"DisplayName", 0, REG_SZ, (BYTE*)displayName, (wcslen(displayName)+1)*sizeof(wchar_t)); RegSetValueExW(hKey, L"DisplayVersion", 0, REG_SZ, (BYTE*)version, (wcslen(version)+1)*sizeof(wchar_t)); RegSetValueExW(hKey, L"Publisher", 0, REG_SZ, (BYTE*)publisher, (wcslen(publisher)+1)*sizeof(wchar_t)); RegSetValueExW(hKey, L"InstallLocation", 0, REG_SZ, (BYTE*)installPath, (wcslen(installPath)+1)*sizeof(wchar_t)); RegSetValueExW(hKey, L"UninstallString", 0, REG_SZ, (BYTE*)uninstallCmd, (wcslen(uninstallCmd)+1)*sizeof(wchar_t)); RegCloseKey(hKey); return TRUE; } // 调用示例 int main() { if (!AddToInstalledApps(L"MyCustomApp", L"我的自定义应用", L"1.0.0", L"个人开发者", L"C:\\Program Files\\MyCustomApp", L"\"C:\\Program Files\\MyCustomApp\\uninstall.exe\"")) { MessageBoxW(NULL, L"添加失败,请确保以管理员身份运行", L"错误", MB_ICONERROR); return 1; } MessageBoxW(NULL, L"已成功添加到已安装应用列表", L"成功", MB_ICONINFORMATION); return 0; }
注意事项
- 所有操作必须以管理员权限运行,否则无法修改系统级注册表项
- 务必提供有效的
UninstallString,否则用户在应用列表点击卸载会报错 - 不要修改系统默认注册表项,只创建属于你的应用的独立子项
- 这套方法仅适用于传统桌面EXE,Windows Store应用的安装信息由系统管理,无法手动添加
内容的提问来源于stack exchange,提问作者wissemzidi
相关产品推荐
相关产品推荐

