简体   繁体   English

Electron:将用户可编辑数据存储在.txt文件中

[英]Electron: Store user editable data in .txt file

I am making an electron app that converts data from.txt files to Javascript arrays.我正在制作一个 electron 应用程序,它将数据从 .txt 文件转换为 Javascript arrays。 This data is stored inside a folder called faces in the main directory.此数据存储在主目录中名为faces的文件夹中。 I also have a button in my app which, when clicked opens file explorer at the faces folder so the user can edit the.txt files.我的应用程序中还有一个按钮,单击该按钮可在faces文件夹中打开文件资源管理器,以便用户可以编辑 .txt 文件。 This works fine when running npm start , but then when I use electron builder to package my app, the app can no longer find the.txt files and the user cannot edit them (giving me lots of errors).这在运行npm start时工作正常,但是当我使用 electron 构建器到 package 我的应用程序时,该应用程序无法再找到 .txt 文件并且用户无法编辑它们(给出很多.txt文件)。 Is there some way to have a folder of.txt files that the app uses to draw information from with Electron builder?有没有办法让应用程序使用 Electron 构建器从中提取信息的 .txt 文件的文件夹?

Edit编辑

Below is the JS used:下面是使用的JS:

//Import Lists from .txt files

var ears = fs.readFileSync('faces/ears.txt', 'utf8').split('\n');
var mouths = fs.readFileSync('faces/mouths.txt', 'utf8').split('\n');
var eyes = fs.readFileSync('faces/eyes.txt', 'utf8').split('\n');

//Opens faces txt docs in file explorer

function edit() {
    shell.openItem(require('electron').remote.app.getAppPath() + '/faces')
}

Here is what happens when I open the packaged app (this is the win-unpacked result but the error is the same for.exe which runs with the installer):这是我打开打包应用程序时发生的情况(这是解压后的结果,但错误与安装程序运行的 for.exe 相同):

截屏

As you can see it does not load an information and you can see it cannot find the faces folder or the.txt files.如您所见,它没有加载信息,您可以看到它找不到faces文件夹或 .txt 文件。

I think the problem is that the path-joining character is different in each OS.我认为问题在于每个操作系统中的路径连接字符都不同。

You can see that every slash was backslash( \ ) before your suffix, /faces .您可以看到每个斜杠在您的后缀/faces之前都是反斜杠 ( \ )。

Try using path module.尝试使用path模块。

const path = require(path)

....

function edit() {
  shell.openItem(
    path.resolve(require('electron').remote.app.getAppPath(), 'faces')
  )
}

The builded app is trying to write in the app folder itself.构建的应用程序正在尝试写入应用程序文件夹本身。 This is possible in dev, but not in production since che app became an asar archive.这在开发中是可能的,但在生产中是不可能的,因为 che app 成为了 asar 档案。 (app.asar). (app.asar)。 If you look at the error your app is trying to write inside app.asas, which is not a folder.如果您查看您的应用程序尝试在 app.asas 中写入的错误,该错误不是文件夹。 So it's not possible to write inside it.所以不可能在里面写。

You probably want to save these kind of information not in the path where the application has been installed, but in some of the paths related with user config/preferences.您可能不希望将这些信息保存在应用程序的安装路径中,而是保存在与用户配置/首选项相关的一些路径中。

This: https://www.electronjs.org/docs/api/app#appgetpathname may help you retrive the right path.这: https://www.electronjs.org/docs/api/app#appgetpathname可以帮助您找回正确的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM