Electron Node.js: 如何将文件安全删除到回收站
要将文件移动到回收站,需要使用 shelljs 模块,它提供了一个 rm 命令,可以将文件移动到回收站。
以下是示例代码:
const shell = require('shelljs');
// 删除文件至回收站
function deleteToRecycleBin(file) {
const os = require('os');
const path = require('path');
const fs = require('fs');
const trashDir = path.join(os.homedir(), '.Trash');
if (!fs.existsSync(trashDir)) {
fs.mkdirSync(trashDir);
}
shell.exec(`mv '${file}' '${trashDir}'`);
}
在此代码中,我们使用了 Node.js 的内置模块 path,以获取回收站路径。然后,我们检查回收站是否存在,如果不存在,则创建回收站。最后,我们使用 shell.exec 函数将文件移动到回收站。
原文地址: http://www.cveoy.top/t/topic/nQjX 著作权归作者所有。请勿转载和采集!