代码之家  ›  专栏  ›  技术社区  ›  alanionita

在Electron中的exe外部创建文件夹

  •  1
  • alanionita  · 技术社区  · 6 年前

    如何在Electron exe外部创建文件夹。

    我计划将应用程序构建为可移植的windows exe,因此我不确定如何获取exe的路径。

    编辑1:

    我试着用 app.getPath("exe"); 在主进程中,但每次运行应用程序时都会出现引用错误 ReferenceError: exe is not defined

    1 回复  |  直到 6 年前
        1
  •  1
  •   alanionita    6 年前

    它确实是app.getPath(“exe”),但必须使用电子事件发射器模式来实现。

    为了访问数据,我触发了主进程上的路径。

    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);
    });
    

    然后在渲染器内部(我使用React),我发出事件并触发一个事件侦听器。

    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}`);
    }