Electron:运行带参数的 shell 命令

rap*_*dko 7 shell electron

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

我可以很容易地使用 shell api ( https://electronjs.org/docs/api/shell )运行 shell 命令

此命令运行完美,例如:

shell.openItem("D:\test.bat");
Run Code Online (Sandbox Code Playgroud)

这个不

shell.openItem("D:\test.bat argument1");
Run Code Online (Sandbox Code Playgroud)

如何使用参数运行电子外壳命令?

Nul*_*Dev 12

shell.openItem不是为此而设计的。从核心模块
使用spawnNodeJS的功能child_process

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
});
Run Code Online (Sandbox Code Playgroud)

  • @SlimaneDeb `child_process` 是 NodeJS 的一部分,而不是 Electron 的一部分。这是一个核心模块。请参阅[此处](https://nodejs.org/api/child_process.html)。 (2认同)