问题
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