Run batch file from Electron main thread(从 Electron 主线程运行批处理文件)
问题描述
我正在尝试从我的电子应用程序中运行一个简单的批处理文件.这是我的代码:
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 主线程运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Electron 主线程运行批处理文件


基础教程推荐
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01