gpt4 book ai didi

electron - 如何从 extraResources 中读取文件?

转载 作者:行者123 更新时间:2023-12-05 07:20:45 27 4
gpt4 key购买 nike

我有一个不需要重建项目就可以更改的文件。因此,我正在使用位于以下位置的 extraResources:

.
├── extraResources
│ └── flags.json
├── src
├── package.json
└── ...

对于构建过程,我的 package.json 包含以下内容:

{
"build": {
"extraResources": [
"./extraResources/**"
]
}
}

这部分似乎有效,因为在构建输出中我在 dist/win-unpacked/resources/extraResources/flags.json 中获得了 extraResources

以上是按照以下步骤完成的:https://stackoverflow.com/a/46033483/2230045

我正在通过以下方式加载此文件:

const filepath = path.join(app.getAppPath(), 'extraResources', 'flags.json');
const data = fs.readFileSync(filepath, 'utf-8');

这在调试期间工作正常,但在构建之后,它失败了:

Uncaught Exception: Error: ENOENT, extraResources\flags.json not found in C:\dev\MyProgram\dist\win-unpacked\resources\app.asar

我不清楚为什么这对其他人似乎有效却不起作用。

基于 here 的提示我尝试使用 hazardous但这没有帮助。

最佳答案

这是因为 appPath 不同:

  • 当您启动开发服务器时,例如C:\dev\MyProgram\dist
  • 当您在 packaged 目录中启动可执行文件时,例如
    C:\dev\MyProgram\packages\win-unpacked\resources\app.asar
    • 并且您指定的额外资源与 app.asar
    • 相邻
    • 例如C:\dev\MyProgram\packages\win-unpacked\resources\extraResources\flags.json

一个简单的解决方法是从路径中删除 asar 部分:

const appPath = app.getAppPath().replace('app.asar', '');
const filepath = path.join(appPath, 'extraResources', 'flags.json');

这在两种情况下都有效:即当它不存在于路径中时,路径不会改变

关于electron - 如何从 extraResources 中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com