Context:
I have an application ExternalApp.exe
that needs to read a file to execute: File.ini
. This file has configurations for the ExternalApp.exe
app and is not human-readable.
上下文:我有一个需要读取文件才能执行的应用程序ExternalApp.exe:File.ini。此文件包含ExternalApp.exe应用程序的配置,不可读。
Solution:
Create an electron
app to read/write File.ini
and execute ExternalApp.exe
.
Build an electron
portable
app using electron-builder
and move the generated .exe (Launcher.exe
) to the ExternalApp
folder.
解决方案:创建一个电子应用程序来读/写File.ini并执行ExternalApp.exe。使用电子生成器构建一个电子便携应用程序,并将生成的.exe(Launcher.exe)移到ExternalApp文件夹中。
We end up with this:
我们最终会得出这样的结论:
[ExternalApp] (folder)
|-- ExternalApp.exe
|-- File.ini
|-- Launcher.exe (electron app)
Issues:
This is working just fine while developing and running npm start
, no issues, but when I compile the app into my executable, tries to read File.ini
from AppData
and not from the ExternalApp
folder.
问题:这在开发和运行NPM Start时运行得很好,没有问题,但当我将应用程序编译到我的可执行文件中时,尝试从AppData而不是从ExternalApp文件夹读取File.ini。
Not sure what I'm doing wrong. Here is the code:
不知道我做错了什么。以下是代码:
Open the external ExternalApp.exe
打开外部ExternalApp.exe
const {shell} = require('electron');
shell.openExternal('./ExternalApp.exe').then(response => {
console.log(response);
});
Result:
结果:
undefined //Instead, the Windows Explorer is opened.
Read File.ini
file
读取File.ini文件
const fs = require('fs');
const file_info = './File.ini';
fs.readFile(file_info , 'utf8', function(error, data){
if (error) throw error;
console.info(data)
});
Result:
结果:
Uncaught Error: ENOENT: no such file or directory, open 'C:\Users\Personal\AppData\Local\Temp\2VBLMOYLXHt0I5WQNGDXIPttjAW\File.ini'
Note: Im new in Electron, this is my first project using it, any help will be much apreciated
注:我是新手在电子,这是我的第一个项目使用它,任何帮助将非常感激
更多回答
shell.openExternal()
is not meant to run executables. Have a look at NodeJS' child_process module. As for the other issue, it seems as though the portable app is first unpacked to a temporary location and then run from there, but I can't confirm that. You could play around and see if app.getAppPath("exe")
helps you to do what you want.
Shell.OpenExternal()不是用来运行可执行文件的。看一看NodeJS的Child_process模块。至于另一个问题,似乎便携应用程序首先被解压到一个临时位置,然后从那里运行,但我不能证实这一点。您可以尝试一下,看看app.getAppPath(“exe”)是否能帮助您做您想做的事情。
I've found the solution in another post, should use PORTABLE_EXECUTABLE_DIR
to get the right path
我在另一篇文章中找到了解决方案,应该使用便携_可执行_目录来获得正确的路径
process.env.PORTABLE_EXECUTABLE_DIR
Thanks @alexander for reviewing it
感谢@Alexander审阅
更多回答
我是一名优秀的程序员,十分优秀!