Electron:如何为用户数据设置自定义目录(--user-data-dir) [英] Electron: How to set a custom directory for user data (--user-data-dir)

查看:100
本文介绍了Electron:如何为用户数据设置自定义目录(--user-data-dir)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的电子应用程序的 --user-data-dir 设置为自定义目录,在我的情况下,我希望它默认为公共目录中的一个文件夹,以便任何运行该应用程序的用户都可以共享相同的资产目录.

看起来不像 Electron 的 appendSwitch() 函数支持这个(我尝试的时候没有用),所以我对如何实现这个开关有点迷茫.

解决方案

在使用 Electron 构建的应用程序中,通常使用 app.getPath(name) 来自主进程:

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

也可以使用 app.setPath(名称,路径):

app.setPath('userData', "path/to/new/directory");

<块引用>

覆盖与关联的特殊目录或文件的路径名称.如果路径指定的目录不存在,则目录将通过此方法创建.失败时出现错误扔了.

您只能覆盖 app.getPath 中定义的名称的路径.

默认情况下,网页的 cookie 和缓存将存储在用户数据目录.如果要更改此位置,则必须在 app 模块的 ready 事件发生之前覆盖 userData 路径发射.

I want to set the --user-data-dir of my electron app to a custom directory, in my case, I would like it to default to a folder in the public directory so any users running the app will share the same asset directory.

It doesn't seem like Electron's appendSwitch() function supports this (and didn't work when I tried), so i'm kind of lost on how to implement this switch.

解决方案

In an application built with Electron, you usually get the default user data directory dynamically by using app.getPath(name) from the main process:

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

It is also possible to set the path to a custom directory by using 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.

这篇关于Electron:如何为用户数据设置自定义目录(--user-data-dir)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆