環境
- デスクトップアプリ制作フレームワーク 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.showItemInFolder
やshell.openItem
はスラッシュのままで問題なく動いたので、気づくまでに少し時間がかかりました……。
※最新版のElectron2.0.2でも試してみましたが同様でした。