2017-03-07 60 views
1

我正在製作一個電子應用程序,用於在運行時啓動存儲在我的計算機上的可執行文件。如果我自己運行該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() 
    } 
}) 

謝謝!

回答

1

您的exe文件是否使用相對路徑來加載字體?如果是這樣,請將exe更改爲更靈活,或者在execFile()的調用中指定可選的第3個參數以指定所需的工作目錄。

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時,您的電子應用是否以不同的用戶身份運行?

+0

它的工作表示感謝! – guavadrag0n