Electron 和 Node.js 本身并不支持将文件移动到 Windows 回收站。但是,您可以使用第三方模块如 shelljs 和 trash 来实现这一点。

  1. 首先,您需要在 Electron 项目中安装这些模块。您可以使用 npm 进行安装。
npm install shelljs trash --save-dev
  1. 在您的 Electron 代码中,导入这些模块。
const shell = require('shelljs');
const trash = require('trash');
  1. 要将某个文件移动到回收站,您可以使用以下代码。
const filePath = 'C:/path/to/file.txt';

if (shell.which('powershell')) {
  shell.exec(`powershell.exe -Command 'Remove-Item -Path '${filePath}' -Force -Recurse'`);
} else {
  trash(filePath);
}

这段代码将首先检查是否安装了 PowerShell。如果是,则使用 PowerShell 的 Remove-Item 命令将文件移动到回收站。否则,它将使用 trash 模块将文件移动到回收站。

请注意,使用这些模块需要一些安全注意事项。例如,您应该始终使用绝对文件路径,以避免意外删除错误文件。

Electron/Node.js: 如何将文件移动到 Windows 回收站

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

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