如何使用 ElectronJS 在资源管理器中打开文件夹

时间:2021-06-22 19:47:17

标签: angular electron

我在 Electron 中使用 Angular 创建一个应用程序来显示文件夹中的内容。这将创建一个文件夹及其子内容的树形表示。

例如结构可以是:

A +
  |
  + B +
  |   |
  |   + C
  |
  + D

要显示路径并在资源管理器中打开文件夹,我使用以下代码:

  1. 组件的 HTML 描述
<a (click)="onButtonClick_OpenExplorer()">{{note.getFolderPath()}}</a>
  1. 组件的打字稿描述
  import { shell, remote} from "electron"
  import * as path from 'path';

  onButtonClick_OpenExplorer(): void {

    // TODO : Solve bug with relative paths

    let folderpath = this.note.getFolderPath()
    console.log("Open: " + folderpath)
    shell.openPath(folderpath)
  }

文件夹路径是相对于应用启动的位置定义的。

当用户点击组件时,会出现奇怪的行为: this.note.getFolderPath() 给出了正确的相对路径 A/B/C(如果用户查看 C 文件夹组件),但 shell 尝试打开:/home/user/Document/A/B/A/B/C。我不知道第一个 A/B 来自哪里,但它抛出了这个错误: gio: file:///home/user/Document/A/B/A/B/C: Error when getting information for file “/home/user/Document/A/B/A/B/C”: No such file or directory

0 个答案:

没有答案