繁体   English   中英

"使用 node.js 执行一个 exe 文件"

[英]Execute an exe file using node.js

我不知道如何在node.js<\/code>中执行exe<\/code>文件。 这是我正在使用的代码。 它不工作,不打印任何东西。 有没有可能使用命令行执行exe<\/code>文件的方法?

var fun = function() {
  console.log("rrrr");
  exec('CALL hai.exe', function(err, data) {

    console.log(err)
    console.log(data.toString());
  });
}
fun();

您可以在 node.js 中尝试子进程模块的 execFile 函数

参考: http : //nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

您的代码应该类似于:

var exec = require('child_process').execFile;

var fun =function(){
   console.log("fun() start");
   exec('HelloJithin.exe', function(err, data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
}
fun();

如果您要执行的exe在其他目录中,并且您的exe对它所在的文件夹有一些依赖性,请尝试在 options 中设置cwd参数

var exec = require('child_process').execFile;
/**
 * Function to execute exe
 * @param {string} fileName The name of the executable file to run.
 * @param {string[]} params List of string arguments.
 * @param {string} path Current working directory of the child process.
 */
function execute(fileName, params, path) {
    let promise = new Promise((resolve, reject) => {
        exec(fileName, params, { cwd: path }, (err, data) => {
            if (err) reject(err);
            else resolve(data);
        });

    });
    return promise;
}

文档

你有没有想过在这个过程中使用批处理文件? 我的意思是使用 node.js 启动一个 .bat 文件,它会同时启动一个 .exe 文件?

只是使用顶部的答案我得到了这个:

  1. 在 exe 文件的目录中创建一个 .bat 文件

  2. 输入 bat 文件START <full file name like E:\\\\Your folder\\\\Your file.exe>

  3. 输入你的 .js 文件: const shell = require('shelljs') shell.exec('E:\\\\Your folder\\\\Your bat file.bat')

如果您使用的是 Node Js 或任何支持 Node JS(React 或 Vue)的前端框架

const { execFile } = require('child_process');

const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});

如果.exe文件位于机器中的某个位置,请将chrome.exe替换为您要执行的应用程序的路径,例如"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"

const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM