How can I get information about the file that launched my app?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 00:48:02

问题


Similar to How to get the arguments for opening file with electron app but the solution there is not working for me.

Using:
OS - Windows 10
Electron - https://github.com/castlabs/electron-releases.git#v1.8.7-vmp1010
electron-builde - v20.28.3

I have a an electron app build with electron-builder, and using the latter I have specified a custom file association, .custom.

So when you double-click on a file with this extension, file.custom, the installed app opens. This file would have some data in it that the app needs, and I'd like to read this data using my app.

Is there any way that my app can detect what launched it, so that I can say "file.custom" launched me, and it's sitting at "C:\Users\Owner\Downloads\,?

The file does not appear in process.argv


回答1:


You can get a reference to the file using process.argv, example:

var ipc = require('ipc');
var fs = require('fs');

// read the file and send data to the render process
ipc.on('get-file-data', function(event) {
    var data = null;
    if (process.platform == 'win32' && process.argv.length >= 2) {
        var openFilePath = process.argv[1];
        data = fs.readFileSync(openFilePath, 'utf-8');
    }
    event.returnValue = data;
});

source: Source



来源:https://stackoverflow.com/questions/52194506/how-can-i-get-information-about-the-file-that-launched-my-app

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