LoginSignup
4
2

More than 5 years have passed since last update.

Electronのshell.moveItemToTrashが失敗する時に試すこと

Posted at

環境

  • デスクトップアプリ制作フレームワーク Electron 1.3.1
  • OS: Windows7

経緯

Electronでファイルをゴミ箱に入れるメソッド、shell.moveItemToTrashを初めて使ったのですが、常にfalseが返ってきてしまい、うまくいきませんでした。

import shell from 'electron';

const fullpath = getFilePath(); // 'c:/path/to/file.js'のような文字列を返す関数
const result = shell.moveItemToTrash(fullpath);
console.log(result); // -> false (失敗)

検索しても解決策が見つからないので色々と試したところ、パスの区切りがスラッシュになっていたのが原因で、バックスラッシュに置換したらちゃんと動作するようになりました。

import path from 'path'; // 追加
import shell from 'electron';

const fullpath = getFilePath().replace(/\//g, path.sep); // 実行環境のパス区切り文字に置換
const result = shell.moveItemToTrash(fullpath);
console.log(result); // -> true (成功)

shell.showItemInFoldershell.openItemはスラッシュのままで問題なく動いたので、気づくまでに少し時間がかかりました……。

※最新版のElectron2.0.2でも試してみましたが同様でした。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2