来自电子项目的可执行文件 运行 未加载资源
Executable run from electron project isn't loading resources
我正在制作一个电子应用程序,它会在 运行 时启动存储在我计算机上的可执行文件。如果我 运行 exe 本身,它可以正常工作并加载所有字体。但是,当电子应用程序 运行 时,它会打开,但无法加载任何字体文件。 exe是visual studio.
制作的编译发布工程
我尝试将 res 文件夹放入与 index.html 相同的目录中,但无济于事。
index.html的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<script>
var child = require('child_process').execFile;
var exePath = "C:\Users\name\Documents\Visual Studio 2015\Projects\Ten\Release\Ten.exe";
var parameters = ["test"];
child(exePath, parameters, function(err, data){
console.log(err);
console.log(data.toString());
})
</script>
</body>
</html>
main.js
代码
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
谢谢!
你的exe是否使用相对路径加载字体?如果是这样,请将 exe 更改为更灵活,或者在调用 execFile()
时指定可选的第三个参数以指定所需的工作目录。
var child = require('child_process').execFile;
var exePath = "C:\Users\name\Documents\Visual Studio 2015\Projects\Ten\Release";
var exe = exePath + "\Ten.exe";
var parameters = ["test"];
var options = {cwd:exePath};
child(exePath, parameters, options, function(err, data){
console.log(err);
console.log(data.toString());
});
如果这不起作用,我猜可能是某种权限问题。您的电子应用程序 运行 与您测试 exe 时的用户不同吗?
我正在制作一个电子应用程序,它会在 运行 时启动存储在我计算机上的可执行文件。如果我 运行 exe 本身,它可以正常工作并加载所有字体。但是,当电子应用程序 运行 时,它会打开,但无法加载任何字体文件。 exe是visual studio.
制作的编译发布工程我尝试将 res 文件夹放入与 index.html 相同的目录中,但无济于事。
index.html的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<script>
var child = require('child_process').execFile;
var exePath = "C:\Users\name\Documents\Visual Studio 2015\Projects\Ten\Release\Ten.exe";
var parameters = ["test"];
child(exePath, parameters, function(err, data){
console.log(err);
console.log(data.toString());
})
</script>
</body>
</html>
main.js
代码const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
谢谢!
你的exe是否使用相对路径加载字体?如果是这样,请将 exe 更改为更灵活,或者在调用 execFile()
时指定可选的第三个参数以指定所需的工作目录。
var child = require('child_process').execFile;
var exePath = "C:\Users\name\Documents\Visual Studio 2015\Projects\Ten\Release";
var exe = exePath + "\Ten.exe";
var parameters = ["test"];
var options = {cwd:exePath};
child(exePath, parameters, options, function(err, data){
console.log(err);
console.log(data.toString());
});
如果这不起作用,我猜可能是某种权限问题。您的电子应用程序 运行 与您测试 exe 时的用户不同吗?