从 Electron 主线程运行批处理文件

2022-01-10 00:00:00 batch-file electron javascript

我正在尝试从我的电子应用程序中运行一个简单的批处理文件.这是我的代码:

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');

相关文章