【问题标题】:Electron: run shell commands with argumentsElectron:运行带参数的 shell 命令
【发布时间】:2019-08-15 04:04:39
【问题描述】:

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

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

此命令运行完美,例如:

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

这个没有

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

如何运行带参数的电子外壳命令?

【问题讨论】:

    标签: shell electron


    【解决方案1】:

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

    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
    });
    

    【讨论】:

    • 它需要安装节点还是只需要 npm 依赖?
    • 我的意思是,“child_process”只是电子 js 的一部分,即:我不需要在客户端桌面上安装 nodejs 吗?
    • @SlimaneDeb child_process 是 NodeJS 的一部分,而不是电子。它是一个核心模块。见here
    • @SlimaneDeb,澄清 NullDev 的声明,电子默认将 nodejs 捆绑在其二进制文件中。因此,您应该可以使用所有核心模块。
    猜你喜欢
    • 2016-12-18
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2015-08-13
    • 2015-02-23
    相关资源
    最近更新 更多