如何使用electron-wix-msi卸载Windows应用时清除本地存储数据
实现Electron应用卸载时自动清除本地存储数据
问题背景
我用Electron.js和npm包electron-wix-msi开发Windows应用,已经集成本地存储且运行正常,现在需要实现:用户卸载应用时自动清除本地存储数据,保证重新安装后无法使用之前的数据。现有代码如下:
const { MSICreator } = require('electron-wix-msi'); const path = require('path'); const iconPath = path.join(__dirname, "./assets/logo/logo.ico"); const msiCreator = new MSICreator({ appDirectory: path.join(__dirname, "./dist/windows/chatbot-win32-x64"), outputDirectory: path.join(__dirname, "./"), // Configure metadata description: 'Chatbot', exe: 'chatbot', name: 'ChatBot', manufacturer: 'manoj', version: '1.0.0', appIconPath: path.join(__dirname, "./assets/logo/logo.ico"), features: { autoLaunch: true, autoUpdate: true }, // Configure installer User Interface ui: { chooseDirectory: true }, }); // 4. Create a .wxs template file msiCreator.create().then(function(){ // Step 5: Compile the template to a .msi file msiCreator.compile(); });
解决方案
核心思路
通过electron-wix-msi的自定义动作功能,在MSI安装包中添加卸载时触发的清理脚本,定位并删除本地存储的文件/文件夹。
具体步骤
确定本地存储路径
根据你的存储方式,找到对应路径:- 用
localStorage的话:路径一般是C:\Users\<用户名>\AppData\Roaming\<应用名称>\Local Storage - 用
electron-store等第三方库:默认在C:\Users\<用户名>\AppData\Roaming\<应用名称>下 - 自定义存储路径:直接用代码里指定的路径
- 用
编写清理脚本
在项目根目录新建cleanup.js,内容如下(以清理electron-store默认路径为例,可根据实际修改):const fs = require('fs'); const path = require('path'); // 获取用户AppData/Roaming目录 const appDataPath = process.env.APPDATA; // 替换成你的应用名称,和MSICreator配置里的name一致 const appStoragePath = path.join(appDataPath, 'ChatBot'); // 递归删除文件夹及内容 function deleteFolderRecursive(targetPath) { if (fs.existsSync(targetPath)) { fs.readdirSync(targetPath).forEach(file => { const curPath = path.join(targetPath, file); if (fs.lstatSync(curPath).isDirectory()) { deleteFolderRecursive(curPath); } else { fs.unlinkSync(curPath); } }); fs.rmdirSync(targetPath); } } try { deleteFolderRecursive(appStoragePath); console.log('本地存储已清理完成'); } catch (err) { console.error('清理失败:', err); }修改MSI配置,添加自定义卸载动作
在MSICreator的配置中加入扩展和自定义动作,让卸载时自动执行清理脚本,修改后的代码:const { MSICreator } = require('electron-wix-msi'); const path = require('path'); const iconPath = path.join(__dirname, "./assets/logo/logo.ico"); const msiCreator = new MSICreator({ appDirectory: path.join(__dirname, "./dist/windows/chatbot-win32-x64"), outputDirectory: path.join(__dirname, "./"), // Configure metadata description: 'Chatbot', exe: 'chatbot', name: 'ChatBot', manufacturer: 'manoj', version: '1.0.0', appIconPath: path.join(__dirname, "./assets/logo/logo.ico"), features: { autoLaunch: true, autoUpdate: true }, // Configure installer User Interface ui: { chooseDirectory: true }, // 添加自定义动作相关配置 extensions: ['WixUtilExtension'], // 允许执行自定义JS动作 customActions: [ { id: 'CleanupStorage', script: path.join(__dirname, 'cleanup.js'), execute: 'deferred', return: 'check', when: 'after', condition: 'REMOVE="ALL"', // 仅完全卸载时执行 target: 'install' } ] }); msiCreator.create().then(function(){ msiCreator.compile(); });验证效果
重新生成MSI安装包,安装后生成一些测试数据,卸载后检查对应存储路径是否被删除即可。
注意事项
- 确保清理脚本的路径正确,避免找不到脚本的问题
- 如果是自定义存储路径,直接在
cleanup.js中替换成你的实际路径 - 测试时注意权限问题,确保卸载过程有足够权限删除文件
内容的提问来源于stack exchange,提问作者Manoj Rana
相关产品推荐
相关产品推荐

