2

How would I create a folder outside the Electron exe.

I'm planning to build the app as a portable windows exe so I'm not sure how to get the path of the exe.

EDIT #1:

I have tried to use app.getPath("exe"); on the main process, but I'm getting a reference error whenever I run the app ReferenceError: exe is not defined

alanionita
  • 1,272
  • 1
  • 16
  • 34
  • 2
    Possible duplicate of [How can I get the path that the application is running with typescript?](https://stackoverflow.com/questions/37213696/how-can-i-get-the-path-that-the-application-is-running-with-typescript) – xmojmr Oct 01 '18 at 10:29
  • @xmojmr Thanks for the link! That does what I want in dev, but in the built app it's returning a ReferenceError. Does it have to used in a certain way or should it be triggered by the renderer process? – alanionita Oct 01 '18 at 13:37

1 Answers1

1

It was indeed app.getPath("exe"), but it has to be implemented using the Electron event emitter pattern.

To have access to the data I triggered the path on the main process.

ipcMain.on("CALL_PRINT_EXE_FILE_PATH", (event) => {
  console.log("printing the file path of the exe");
  const exePath = app.getPath("exe");
  console.log(`exePath: ${exePath}`);
  mainWindow.send("PRINT_EXE_FILE_PATH", exePath);
});

Then inside the renderer (I use React), I emit the event and also trigger an event listener.

const { ipcRenderer } = window.require("electron");
...
componentDidMount() {
  ipcRenderer.send("CALL_PRINT_EXE_FILE_PATH");
}
componentWillMount() {
  ipcRenderer.on("PRINT_EXE_FILE_PATH", this.handlePrintExePath);
}

componentWillUnmount() {
  ipcRenderer.removeListener("PRINT_EXE_FILE_PATH", this.handlePrintExePath);
}
...
handlePrintExePath(event, exePath) {
  console.log("printing the app exe in the render");
  console.log(`exeFilePath: ${exePath}`);
}
alanionita
  • 1,272
  • 1
  • 16
  • 34