【问题标题】:Electron / Node file path locationElectron/Node 文件路径位置
【发布时间】:2019-04-05 00:17:20
【问题描述】:

我有一个电子应用程序,它使用electron-log 来处理为应用程序创建一些调试信息。默认情况下,它会将文件保存到每个模块的以下位置:

**on macOS:** ~/Library/Logs/<app name>/log.log

**on Windows:** %USERPROFILE%\AppData\Roaming\<app name>\log.log

我在菜单中添加了“查看调试信息”选项。我的目标是将此日志读入文本区域(在渲染器上),以便他们可以在需要时提供支持。

在我的渲染器中,我使用fs 来访问文件系统,但我在process.env 中找不到指向这些位置的任何内容,所以我认为它们是自定义的?

我是否缺少包含操作系统上这些路径的变量?

const fs = require('fs');

if(process.platform == 'darwin'){
    // Path is ~/Library/Logs/<app name>/log.log
    // Read the file into the textarea
}else{
    // Path is %USERPROFILE%\AppData\Roaming\<app name>\log.log
    // Read the file into the textarea
}

【问题讨论】:

    标签: node.js electron


    【解决方案1】:

    试试这个:

    const log = require('electron-log');
    
    const path = log.transports.file.findLogPath();
    

    findLogPath.js#L17

    【讨论】:

      【解决方案2】:

      我认为你可以使用app.getPath(...)

      import { app } from "electron";
      let logFileName;
      
      // If, darwin; PATH is: ~/Library/Logs/<app name>/log.log
      if(process.platform == 'darwin'){
        logFileName = app.getPath("logs") + "/log.log",
      } else if(process.platform == 'win32'){ {
        logFileName = app.getPath("userData") + "/log.log",
      } else {
        // Handle other supported platforms ('aix','freebsd', 'linux', 'openbsd', 'sunos')
      }
      
      fs.readFile(logFileName, function read(err, data) {
        if (err) {
          throw err;
        }
        // Read the file data content into the text-area.
      });
      

      来自 Electron 的 getPath documentation

      app.getPath(name)

      姓名String

      返回String - 与名称关联的特殊目录或文件的路径。失败时,抛出一个错误。

      您可以通过name请求以下路径:

      home用户的主目录。

      appData 每用户应用程序数据目录,默认指向:Windows 上的%APPDATA%,Linux 上的$XDG_CONFIG_HOME~/.config 以及macOS 上的~/Library/Application Support

      userData 存放应用配置文件的目录,默认为appData目录,后面加上你的应用名称。

      temp临时目录。

      exe当前的可执行文件。

      module libchromiumcontent 库。

      desktop当前用户的桌面目录。

      documents 用户“我的文档”的目录。

      downloads 用户下载目录。

      music 用户音乐目录。

      pictures 用户图片目录。

      videos 用户视频目录。

      logs 应用程序日志文件夹的目录。

      pepperFlashSystemPlugin Pepper Flash 插件系统版本的完整路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-11
        • 1970-01-01
        • 1970-01-01
        • 2023-02-23
        • 2013-04-25
        • 2016-11-26
        • 2021-10-19
        • 2016-07-25
        相关资源
        最近更新 更多