Electron:如何为用户数据设置自定义目录(--user-data-dir)

Electron: How to set a custom directory for user data (--user-data-dir)

我想将我的电子应用程序的 --user-data-dir 设置为自定义目录,在我的例子中,我希望它默认为 public 目录中的一个文件夹,这样任何用户运行 应用将共享相同的资产目录。

Electron 的 appendSwitch() 函数似乎不支持这个(我尝试时没有用),所以我有点不知道如何实现这个开关。

在使用 Electron 构建的应用程序中,您通常通过从主进程使用 app.getPath(name) 动态获取默认用户数据目录:

const { app } = require ('electron');
const userDataPath = app.getPath ('userData');

也可以使用 app.setPath(name, path):

将路径设置为自定义目录
app.setPath ('userData', "path/to/new/directory");

Overrides the path to a special directory or file associated with name. If the path specifies a directory that does not exist, the directory will be created by this method. On failure an Error is thrown.

You can only override paths of a name defined in app.getPath.

By default, web pages' cookies and caches will be stored under the userData directory. If you want to change this location, you have to override the userData path before the ready event of the app module is emitted.