How to open a folder in the explorer with ElectronJS

Viewed 262

I'm using Angular in Electron to create an app to show content in folders. This create a tree representation of a folder and its subcontent.

For instance the structure could be:

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

To show the path and to open the folder in the explorer, I use this code:

  1. HTML description of the component
<a (click)="onButtonClick_OpenExplorer()">{{note.getFolderPath()}}</a>
  1. Typescript description of the component
  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)
  }

The folderpath is defined relatively from where the app is launched.

When the user clicks on the component a curious behaviour occurs: this.note.getFolderPath() gives the correct relative path A/B/C (if the user looks at the C folder component), but the shell tries to open: /home/user/Document/A/B/A/B/C. I don't know where the first A/B comes from but it throws this error: 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 Answers
Related