How to get the arguments for opening file with electron app

偶尔善良 提交于 2019-12-24 06:08:50

问题


Perhaps I'm being stupid but I can't seem to find any documentation on how to get the startup arguments for an electron app. My scenario is something like this:

  • Right-click file in Windows Explorer
  • Open with -> My electron app
  • Electron app opens and can work with the file

I can get the electron app to open, but how do I work with the file that was right-clicked?


回答1:


Assuming that you have the "Open with" portion working, Windows will pass the filename as a command line argument. So just get the file name/path from process.argv

if(process.argv.length >= 2) {
    let filePath = process.argv[1];
    //open, read, handle file
}



回答2:


try {
        var electron = require('electron');
        var app = electron.remote;
        if (app.process.platform == 'win32' && app.process.argv.length >= 2) {
            var openFilePath = app.process.argv[1];
            if (openFilePath !== "") {
                console.log(openFilePath);
            }
        }
    } catch (e) {
    }


来源:https://stackoverflow.com/questions/44828843/how-to-get-the-arguments-for-opening-file-with-electron-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!