ElectronJS 本身并没有提供直接删除文件至 Windows 回收站的方法,但可以使用 Node.js 的 fs 模块和 shell 模块来实现该功能。

首先,需要使用 fs 模块的 unlink 方法删除文件,然后使用 shell 模块的 moveItemToTrash 方法将文件移动到回收站。

以下是一个示例代码:

const { shell } = require('electron');
const fs = require('fs');

const filePath = 'C:\\path\\to\\file.txt'; // 文件路径

fs.unlink(filePath, (err) => {
  if (err) {
    console.error(err);
    return;
  }
  shell.moveItemToTrash(filePath);
});

在上面的代码中,首先使用 fs.unlink 方法删除文件,如果出现错误则输出错误信息。然后使用 shell.moveItemToTrash 方法将文件移动到回收站。注意,shell 模块只能在主进程中使用,因此需要将上面的代码放在主进程中执行。

需要注意的是,moveItemToTrash 方法只能在 macOS 和 Windows 系统上使用,Linux 系统上没有回收站功能

electronjs 删除文件至win回收站

原文地址: https://www.cveoy.top/t/topic/ejgE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录