問題描述
我正在嘗試從我的電子應用程序中運行一個簡單的批處理文件.這是我的代碼:
I'm attempting to run a simple batch file from my electron application. Here is my code:
globalShortcut.register('Control+B', () => {
log.info('Batch File Triggered: ' + app.getAppPath() + '\local\print.bat')
require('child_process').exec(app.getAppPath() + '\local\print.bat', function (err, stdout, stderr) {
if (err) {
// Ooops.
// console.log(stderr);
return console.log(err);
}
// Done.
console.log(stdout);
});
})
當用戶按下 Control+B 時應該會觸發批處理文件,但它不起作用.生成了日志條目,并且我驗證了路徑是正確的,但該文件從未真正啟動過.
The batch file should be triggered when Control+B is pressed by the user, but it does not work. The log entry is made, and I've verified the path is correct, but the file is never actually launched.
我發現了這些問題,它們提出了同樣的問題,但這些問題已經 4 歲了,沒有一個答案對我有用,沒有顯示,沒有錯誤,什么都沒有.
I found these questions, which ask the same question, but these are 4 years old at this point and none of the answers have worked for me, there is no display, no error, nothing.
- 從node.js運行一個windows批處理文件
- http://stackoverflow.com/questions/21557461/execute-a-batch-file-from-nodejs
我也嘗試過 child_process.spawn,但也沒有什么明顯的效果.
I've also tried the child_process.spawn but that also did nothing noticeable.
var ls = spawn('cmd.exe', ['/c', app.getAppPath() + '\local\print.bat']);
如何從電子應用程序中啟動批處理文件?
How can I launch my batch file from my electron application?
推薦答案
我剛剛發現了一個如此簡單的方法來做到這一點.您可以使用電子外殼模塊,如下所示:
I've just discovered such an easy way to do this. You can use the electron shell module, like this:
const {shell} = require('electron');
// Open a local file in the default app
shell.openItem(app.getAppPath() + '\local\print.bat');
這篇關于從 Electron 主線程運行批處理文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!