gpt4 book ai didi

shell - Electron:运行带参数的 shell 命令

转载 作者:行者123 更新时间:2023-12-05 00:10:58 26 4
gpt4 key购买 nike

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

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

此命令运行完美,例如:

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

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

如何使用参数运行 Electron shell 命令?

最佳答案

shell.openItem不是为此而设计的。
使用 spawn来自 child_process 的 NodeJS 功能核心模块。

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 - Electron:运行带参数的 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com