电子:使用参数运行shell命令 [英] Electron: run shell commands with arguments

查看:56
本文介绍了电子:使用参数运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个电子应用程序,

I'm building an electron app,

我可以使用shell api轻松地运行shell命令( https://electronjs.org/docs/api/shell

I can run shell commands pretty easily with the shell api (https://electronjs.org/docs/api/shell)

此命令运行完美例如:

shell.openItem("D:\test.bat");

这不是

shell.openItem("D:\test.bat argument1");

如何通过参数运行电子外壳命令?

How to run electron shell command with arguments?

推荐答案

shell.openItem 并非为此目的而设计。

child_process 核心模块使用NodeJS的 spawn 函数。

shell.openItem isn't designed for that.
Use the spawn function of NodeJS from the child_process core module.

let spawn = require("child_process").spawn;

let bat = spawn("cmd.exe", [
    "/c",          // Argument for cmd.exe to carry out the specified script
    "D:\test.bat", // Path to your file
    "argument1",   // First argument
    "argumentN"    // n-th argument
]);

bat.stdout.on("data", (data) => {
    // Handle data...
});

bat.stderr.on("data", (err) => {
    // Handle error...
});

bat.on("exit", (code) => {
    // Handle exit
});

这篇关于电子:使用参数运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆