如何将文件保存到电子js中的应用程序文件夹

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

我本来想把申请文件保存到C:\Users\<username>\AppData\Local。但后来我意识到它不适用于 MAC。有没有办法将文件保存到为应用程序自动创建的文件夹中,或者如何为 windows、mac 和 linux

创建一个文件夹

你的问题有点乱。 Electron 提供了一种跨平台的方式来获取各种 OS 特定目录的路径:app.getPath(name)。这使开发人员能够使用相同的代码来引用这些公共目录。 Electron 的文档相当不错。

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.