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

Run batch file from Electron main thread(从 Electron 主线程运行批处理文件)
本文介绍了从 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 主线程运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)