Electron:运行带参数的 shell 命令 [英] Electron: run shell commands with arguments

查看:45
本文介绍了Electron:运行带参数的 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)

此命令运行完美,例如:

This command runs perfect for example:

shell.openItem("D:	est.bat");

这个没有

shell.openItem("D:	est.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:	est.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
});

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

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