how to save a file to the application folder in electron js

Viewed 841

I originally wanted to save application files to C:\Users\<username>\AppData\Local. But then I realized it wouldn't work for MAC. Is there a way to save the file to an automatically created folder for the application or how to create one for windows, mac and linux

1 Answers

You question is a little confusing. Electron provides a cross-platform way to get the paths to various OS-specific directories: app.getPath(name). This enables the developer to use the same code to reference these common directories. Electron's documentation is pretty decent.

app.getPath(name)

name String - 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.
    • cache
    •temp Temporary directory.
    • exe The current executable file.
    • module The libchromiumcontent library.
    • desktop The current user's Desktop directory.
    • documents Directory for a user's "My Documents".
    • downloads Directory for a user's downloads.
    • music Directory for a user's music.
    • pictures Directory for a user's pictures.
    • videos Directory for a user's videos.
    • recent Directory for the user's recent files (Windows only).
    • logs Directory for your app's log folder.
    • crashDumps Directory where crash dumps are stored.

Returns String - A path to a special directory or file associated with name. On failure, an Error is thrown.

If app.getPath('logs') is called without called app.setAppLogsPath() being called first, a default log directory will be created equivalent to calling app.setAppLogsPath() without a path parameter.

Related