gpt4 book ai didi

javascript - 如何在退出时删除所有 Electron 膨胀软件?

转载 作者:行者123 更新时间:2023-12-01 15:25:22 26 4
gpt4 key购买 nike

当 Electron 应用程序退出时,如何从“AppData\roaming\MyApp”文件夹中删除 cookie、本地存储和其他垃圾?
我尝试在应用程序退出时删除整个目录,但它引发了 EBUSY 错误。显然文件被锁定或其他东西,几乎就像有人不希望我们能够消除膨胀?

const fs = require('fs-extra');

const clearBloat = async () => fs.remove(path.join(app.getPath('appData'), app.name));

app.on('window-all-closed', async () => {
await clearBloat();
});

最佳答案

经过一些测试,我发现您必须在 Electron 过程结束后删除文件(尝试在 中删除文件时退出 will-quit app events 不会删除文件/文件夹;它们会立即重新创建。 Electron 中的某些东西(可能是 Chromium)希望这些文件/文件夹在应用程序运行时存在,并且弄清楚如何 Hook 它的工作量太大)。
对我有用的是从等待 3 秒的 shell 中生成一个分离的 cmd,然后删除给定应用程序文件夹中的所有文件/文件夹。读者的练习将是隐藏 ping 命令的输出(或隐藏窗口,但前面有 mixed success),或选择不同的命令。我发现 timeout 有效,但 sleep choice (即 like this )不起作用。
以下是您需要添加的内容:

const { app } = require("electron");
const { spawn } = require("child_process");
const path = require("path");

...

app.on("will-quit", async (event) => {
const folder = path.join(app.getPath("appData"), app.name);

// Wait 3 seconds, navigate into your app folder and delete all files/folders
const cmd = `ping localhost -n 3 > nul 2>&1 && pushd "${folder}" && (rd /s /q "${folder}" 2>nul & popd)`;

// shell = true prevents EONENT errors
// stdio = ignore allows the pipes to continue processing w/o handling command output
// detached = true allows the command to run once your app is [completely] shut down
const process = spawn(cmd, { shell: true, stdio: "ignore", detached: true });

// Prevents the parent process from waiting for the child (this) process to finish
process.unref();
});
正如另一位用户所提到的,您的 a method 上有 electron session 可用,这是一个清除所有这些文件/文件夹的 native API。但是,它返回了一个 promise ,我无法弄清楚如何在这些应用程序事件之一中同步执行它。
Reference #1

关于javascript - 如何在退出时删除所有 Electron 膨胀软件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63834076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com