使用NodeJS和Electron使用OS的默认应用程序(带有Word的docx等)打开外部文件

Som*_*der 8 javascript node.js electron

我正在使用NodeJS/Electron作为桌面应用.

我想做的是用它的OS'默认应用程序打开一个文件,比如.docx和Word.

到目前为止我尝试的是使用child_process.spawn,.exec或.execFile的方法,但我没有得到任何东西.

这是我的实际代码:

var fs = require('fs'),
    cp = require('child_process');

cp.spawn(__dirname + '/test.docx');
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Vad*_*gon 17

使用openItem()Electron shell模块提供的功能,例如:

const shell = require('electron').shell;
const path = require('path');

shell.openItem(path.join(__dirname, 'test.docx'));
Run Code Online (Sandbox Code Playgroud)

根据文档,shell模块应该在主/浏览器和渲染器进程中都可用.

  • @django 不,这是一种一劳永逸的事情。如果您想检测外部应用程序是否已关闭,则需要使用 [child_process.spawn](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options )。 (2认同)