问题
I want to store images on the users computer, so I figure it should be stored in users data folder, as described here.
app.getPath(name)
name
. Returns String - A path to a special directory or file associated with name. On failure an Error is thrown. You can request the following paths by the name:
home
User's home directory
appData
Per-user application data directory, which by default points to:%APPDATA% on Windows $XDG_CONFIG_HOME or ~/.config on Linux ~/Library/Application Support on macOS
userData
The directory for storing your app's configuration files, which by default it is the appData directory appended with your app's name.- ...
This is what I think you're supposed to do:
const app = require('electron');
alert(app.getPath('userData'));
But I get "getPath is not a function". I am not sure where to put it. It does not work from my html file or the renderer file, and I'm not sure how to use it from the main file because that's not linked to the web page.
回答1:
Figured it out thanks to this
const remote = require('electron').remote;
const app = remote.app;
console.log(app.getPath('userData'));
回答2:
Here is what I use when I need to switch between dev and release
const electron = require('electron');
export const userDataPath = (electron.app || electron.remote.app).getPath(
'userData'
);
回答3:
Another way to prevent the error "getPath is not a function" is to make the code work both in the renderer process and the main process:
const electron = require('electron');
const configDir = (electron.app || electron.remote.app).getPath('userData');
console.log(configDir);
来源:https://stackoverflow.com/questions/47957479/how-to-use-electrons-app-getpath-to-store-data