How to get the original path of a portable Electron app?

前端 未结 7 1822
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 08:08

I have an Portable Electron App (packed with: electron-builder + asar, portable build) on Windows. I try to get the application path but it returns a path within the user\\t

相关标签:
7条回答
  • 2020-12-24 08:39

    I found a solution: Use the Environment Variable (created by Electron-Builder)

    process.env.PORTABLE_EXECUTABLE_DIR
    

    to show the real Path of the App.exe. Works only packed with Electron-Builder

    0 讨论(0)
  • 2020-12-24 08:40

    I had a lot of trouble with this and was finally able to solve the issue by replacing __dirname by '.', see working example below :

    const path = require('path')
    const myAppPath = path.resolve('.', 'myapp.exe');
    
    0 讨论(0)
  • 2020-12-24 08:49

    None of the above answers worked for me on Windows 10.

    process.env.PORTABLE_EXECUTABLE_DIR returns the Temp directory path.

    I got it to work using:

    process.env.INIT_CWD
    
    0 讨论(0)
  • 2020-12-24 08:53

    Seems like PORTABLE_EXECUTABLE_FILE only works under certain Electron-Builder configurations; In particular, it won't work in apps deployed as a portable directory.

    In such cases, the following will get you the application root path:

    const path = require('path')
    import { app } from 'electron'
    
    let rootDir = app.getAppPath()
    let last = path.basename(rootDir)
    if (last == 'app.asar') {
        rootDir = Path.dirname(app.getPath('exe'))
    }
    
    0 讨论(0)
  • 2020-12-24 08:57
    process.env.PORTABLE_EXECUTABLE_FILE
    

    will give you the full path to the file.

    0 讨论(0)
  • 2020-12-24 08:58

    From the main process:

    // If not already defined...
    const { app } = require ('electron');
    const path = require ('path');
    
    let execPath;
    
    execPath = path.dirname (app.getPath ('exe'));
    // or
    execPath = path.dirname (process.execPath);
    

    From a renderer process:

    // If not already defined...
    const { remote } = require ('electron');
    const path = require ('path');
    
    let execPath;
    
    execPath = path.dirname (remote.app.getPath ('exe'));
    // or
    execPath = path.dirname (remote.process.execPath);
    
    0 讨论(0)
提交回复
热议问题