最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron store my app datas in 'userData' path - Stack Overflow

programmeradmin1浏览0评论

I'm building and trying do deploying a packaged electron app. FOr the packaging i used

  • electron-packager
  • electron-installer-debian
  • electron-installer-dmg
  • electron-winstaller

and I'm facing a little issue where I have to store tha appa datas somewhere in my user puter.

I saw that the good practice is to use the the folder in the path that is returned by the electron method app.getPath('userData').

from the docs It is The directory for storing the app's configuration files, which by default it is the appData directory appended with the app name.

%APPDATA% on Windows
$XDG_CONFIG_HOME or ~/.config on Linux
~/Library/Application Support on macOS

By my tests sometimes this folder is not created automatically when the app is installed and other times yes and I'm wondering if i should create it or not.

Right now i'm quitting the app if this folder isn't present in the pc with the following code

var DatasPath = app.getPath('userData')
if (!fs.existsSync(DatasPath)){
      process.exit()
  }

So the question is

  • should i create the DatasPath folder with fs.mkdirSync(DatasPath); when it is not present or it is 'bad practice to do so', and if I can create the folder i have to warning the user the i have just added that folder?

I'm building and trying do deploying a packaged electron app. FOr the packaging i used

  • electron-packager
  • electron-installer-debian
  • electron-installer-dmg
  • electron-winstaller

and I'm facing a little issue where I have to store tha appa datas somewhere in my user puter.

I saw that the good practice is to use the the folder in the path that is returned by the electron method app.getPath('userData').

from the docs It is The directory for storing the app's configuration files, which by default it is the appData directory appended with the app name.

%APPDATA% on Windows
$XDG_CONFIG_HOME or ~/.config on Linux
~/Library/Application Support on macOS

By my tests sometimes this folder is not created automatically when the app is installed and other times yes and I'm wondering if i should create it or not.

Right now i'm quitting the app if this folder isn't present in the pc with the following code

var DatasPath = app.getPath('userData')
if (!fs.existsSync(DatasPath)){
      process.exit()
  }

So the question is

  • should i create the DatasPath folder with fs.mkdirSync(DatasPath); when it is not present or it is 'bad practice to do so', and if I can create the folder i have to warning the user the i have just added that folder?
Share Improve this question edited Jul 13, 2020 at 14:48 tpikachu 4,8442 gold badges21 silver badges45 bronze badges asked Apr 5, 2020 at 8:16 Mat.CMat.C 1,4393 gold badges26 silver badges49 bronze badges 2
  • Without seeing your code, it is impossible to say what might be causing a crash. Post your code. – spring Commented Apr 5, 2020 at 14:24
  • @NoGrabbing Sorry for the bad explanation, the crash was an intentional crash caused by the Absence of this folder. i don't know if i'm supposed to create it or not so i automatically make th app quit if there is not that folder. – Mat.C Commented Apr 5, 2020 at 14:29
Add a ment  | 

4 Answers 4

Reset to default 6

(Expanding my reply from a "ment" to an "answer")

i don't know if i'm supposed to create it or not so i automatically make the app quit if there is not that folder

It seems you are taking "userData" too literally? It is not an actual "folder" named "userData – it is a path to where the operating system stores data for that application. Electron currently runs on 3 operating systems and each one does things differently. For our convenience, Electron hides those differences by creating the wrapper method app.getPath(name) so the same code will work on each OS.

Try this: put the line below in your main.js script:

console.log(app.getPath('userData'));

/Users/*********/Library/Application Support/MyCoolApp

(the "*********" will be your user account name.)

UPDATED:

Run the code below in main.js and then look in the folder specified by the "userData" path

const fs = require("fs");
const path = require('path');
var datasPath = app.getPath('userData')
var data = "I am the cheese"
var filePath = path.join(datasPath, "savedData.txt")
fs.writeFileSync(filePath, data)

At pathConfig.js

function getAppDataPath() {
  switch (process.platform) {
    case "darwin": {
      return path.join(process.env.HOME, "Library", "Application Support", "myApp");
    }
    case "win32": {
      return path.join(process.env.APPDATA, "myApp");
    }
    case "linux": {
      return path.join(process.env.HOME, ".myApp");
    }
    default: {
      console.log("Unsupported platform!");
      process.exit(1);
    }
  }
}

const appPath = __dirname;
const appDataPath =
  !process.env.NODE_ENV || process.env.NODE_ENV === "production"
    ? getAppDataPath() // Live Mode
    : path.join(appPath, "AppData"); // Dev Mode

if (!fs.existsSync(appDataPath)) {
    // If the AppData dir doesn't exist at expected Path. Then Create
    // Maybe the case when the user runs the app first.
    fs.mkdirSync(appDataPath);
}

In each operating system the appData folder has a different path and the perfect way of getting this path is by calling app.getPath('userData') in the main process. But there is a package that can handle this for you, it stores data in a JSON file and update it in every change. In my opinion this package is much better than handling everything by your self. Read more : https://www.npmjs./package/electron-data-holder

Just to clarify : Actually appdata/yourApp or Application Support/yourApp etc..

Will be created for sure because it is holding a lot of files and folders used by Chromium and ElectronJs to work. Like caches, cookies etc...

/Users/home/Library/Application Support/MyApp :

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>