如何在 Node Webkit 中 运行 外部 exe?

How to run external exe in Node Webkit?

我正在为我的网络应用程序使用 Node Webkit,我真的是一个使用 Node Webkit 的新手。我想在我的应用程序中 运行 我的 exe,但我什至无法使用 'child_process' 打开简单的记事本。我在网站上看到了一些例子,但我仍然觉得很难 运行 notepad.exe,请提前帮助并非常感谢你。

var execFile = require 
('child_process').execFile, child;

child = execFile('C:\Windows\notepad.exe',
function(error,stdout,stderr) { 
if (error) {
            console.log(error.stack); 
            console.log('Error code: '+ error.code); 
            console.log('Signal received: '+ 
            error.signal);
           } 
console.log('Child Process stdout: '+ stdout);
console.log('Child Process stderr: '+ stderr);
 }); 
child.on('exit', function (code) { 
console.log('Child process exited '+
'with exit code '+ code);
});

我还尝试 运行 使用 meadco-neptune 插件执行 exe 并添加插件 我将代码放入 package.json 文件,但它显示无法加载插件。我的 package.json 文件是这样的

 {
   "name": "sample",
   "version": "1.0.0",
   "description": "",
   "main": "index.html",
   "window": {
   "toolbar": false,
   "frame": false,
   "resizable": false,
   "show": true,

   "title": " example"
             },
   "webkit": {
   "plugin": true
             },
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
             },
    "author": "xyz",
    "license": "ISC"
 }

在node.js中有两种使用标准模块child_process启动外部程序的方法:execspawn

使用 exec 时,您会在外部程序退出时获得 stdout 和 stderror 信息。正如 Mi Ke Bu 在评论中正确指出的那样,数据才 returned 到 node.js。

但是如果你想交互地从外部程序接收数据(我怀疑你真的不会启动notepad.exe),你应该使用另一种方法 - spawn.

考虑示例:

var spawn = require('child_process').spawn,
    child    = spawn('C:\windows\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);

child.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

child.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

你还需要在路径名中使用双反斜杠:C:\Windows\notepad.exe,否则你的路径被评估为
C:windows notepad.exe (第 return 行)当然不存在。

或者您可以只使用正斜杠,如示例中的命令行参数。