我正在尝试从我的 Electron 应用程序运行一个简单的批处理文件。这是我的代码:
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 时触发,但它不起作用。日志条目已创建,我已验证路径正确,但文件从未实际启动。
我发现了这些问题,它们问的是同一个问题,但这些问题已经 4 年了,没有一个答案对我有用,没有显示,没有错误,什么都没有。
- > run a windows batch file from node.js
- > http://stackoverflow.com/questions/21557461/execute-a-batch-file-from-nodejs
我也尝试过 child_process.spawn,但也没有任何明显效果。
var ls = spawn('cmd.exe', ['/c', app.getAppPath() + '\\local\\print.bat']);
如何从我的 Electron 应用程序启动我的批处理文件?
最佳答案
我刚刚发现了一个如此简单的方法来做到这一点。您可以像这样使用 electron shell 模块:
const {shell} = require('electron');
// Open a local file in the default app
shell.openItem(app.getAppPath() + '\\local\\print.bat');
关于javascript - 从 Electron 主线程运行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906156/