[英]Persistent file storage across electon app updates with electron-builder electron-updater
When I update an Electron app using the electron-builder autoUpdater , all the file storage I defined is overwritten.当我使用electron-builder autoUpdater更新 Electron 应用程序时,我定义的所有文件存储都被覆盖。 What settings do I need to make these files persist?
我需要哪些设置才能使这些文件持久存在?
Here is an MCVE example (for the main process):这是一个 MCVE 示例(针对主进程):
const { app, BrowserView, BrowserWindow,ipcMain } = require('electron');
const log = require("electron-log");
const { autoUpdater } = require("electron-updater");
const fs = require( 'fs');
const path = require('path');
let win
let rand = String(Math.floor(Math.random() * 10000));
console.log('Generated random number',rand)
app.whenReady().then(async () => {
win = new BrowserWindow({
fullscreen: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html');
setInterval(function() {
let a = autoUpdater.checkForUpdatesAndNotify();
}, 60000);
let exists = false;
fs.stat('persistentFile',(err, stats) => {
if (err == null){
exists = true
}
if (!exists){
fs.writeFile('persistentFile',rand,(err)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent file has been created!',rand);
console.log('Persistent file has been created!',rand);
})
}
else {
fs.readFile('persistentFile',(err,data)=>{
if (err) throw err;
win.webContents.send('console_message', 'Persistent already exists',data);
console.log('Persistent already exists',data);
})
}
})
win.webContents.send('console_message', 'Random No is' + String(rand));
})
In this example I'd like the file persistentFile
to persist across updates, so that it contains the number generated from the first version.在此示例中,我希望文件
persistentFile
在更新过程中保持不变,以便它包含从第一个版本生成的数字。 However, at the moment, the file is overwritten every update.但是,目前,每次更新都会覆盖该文件。
How would you ensure persistent file storage across electron-builder autoupdates?您将如何确保跨 electron-builder 自动更新的持久文件存储?
I managed to do this after looking at Cameron Nokes' excellent blog:在查看 Cameron Nokes 的优秀博客后,我设法做到了这一点:
cameronnokes.com/blog/how-to-store-user-data-in-electron : cameronnokes.com/blog/how-to-store-user-data-in-electron :
Storing user data in the operating system's designated location for user's app data is the idiomatic way for native app's to persist user data because:
将用户数据存储在操作系统为用户应用程序数据指定的位置是本机应用程序持久保存用户数据的惯用方式,因为:
when we auto-update the app, our source files may get moved or delete
当我们自动更新应用程序时,我们的源文件可能会被移动或删除
changing or adding to an app's internal files will invalidate the code signature
更改或添加到应用程序的内部文件将使代码签名无效
To do this I used const userDataPath = app.getPath('userData');
为此,我使用了
const userDataPath = app.getPath('userData');
to get a path to the OS' app storage location.获取操作系统应用程序存储位置的路径。 and then changed the references to
'persistentFile'
to String(userDataPath)+'/persistentFile'
:然后将对
'persistentFile'
的引用更改为String(userDataPath)+'/persistentFile'
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.