使用 NodeJS 列出正在运行的应用程序

Cam*_*ron 5 node.js

使用 NodeJS 我想获取 Windows 上打开的应用程序列表。

类似的东西:

exec("tasklist", function (error, stdout, stderr) {

    for(var i=0;i<stdout.length;i++)
    {
        if( stdout[i]['name'].indexOf('ll_') > -1 )
        {
            appList.push({'id':stdout[i]['id'],'name':stdout[i]['name']});
        }
    }

});
Run Code Online (Sandbox Code Playgroud)

如果appList应用程序的 ID 和名称以ll_.

我怎样才能做到这一点?

rob*_*lep 6

(我不运行 Windows,所以以下未经测试)

首先,安装tasklist

$ npm install tasklist
Run Code Online (Sandbox Code Playgroud)

然后,使用以下脚本:

var tasklist = require('tasklist');

tasklist(function(err, tasks) {
  if (err) throw err; // TODO: proper error handling
  var appList = tasks.filter(function(task) {
    return task.imageName.indexOf('ll_') === 0;
  }).map(function(task) {
    return {
      id   : task.pid, // XXX: is that the same as your `id`?
      name : task.imageName,
    };
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,你的回答有帮助。然而,为了将来的参考,“tasklist”模块似乎已经改变,需要与承诺而不是回调一起使用。所以调用它是“tasklist().then(function(tasks) { ..... });” (2认同)