从电子项目运行的可执行文件未加载资源



我正在制作一个电子应用程序,该应用程序在运行时启动存储在计算机上的可执行文件。如果我单独运行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()中指定可选的第3 arg以指定所需的工作目录。

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时作为其他用户的运行吗?

最新更新