我将 NodeJS/Electron 用于桌面应用程序.
I'm using NodeJS/Electron for a desktop app.
我想做的是用它的操作系统的默认应用程序打开一个文件,比如用 Word 的 .docx.
What I wanna do, is to open a file with it's OS' default application, like .docx with Word.
到目前为止,我尝试的是使用 child_process.spawn、.exec 或 .execFile 的方法,但我什么也没得到.
What I tried so far are approaches using child_process.spawn, .exec or .execFile but I don't get anything.
这是我的实际代码:
var fs = require('fs'),
cp = require('child_process');
cp.spawn(__dirname + '/test.docx');
提前致谢.
使用openItem() 函数由 Electron 的 shell 模块提供,例如:
Use the openItem() function provided by Electron's shell module, for example:
const shell = require('electron').shell;
const path = require('path');
shell.openItem(path.join(__dirname, 'test.docx'));
根据文档,shell 模块应该在主/浏览器和渲染器进程中都可用.
According to the docs the shell module should be available in both the main/browser and renderer processes.
注意:Electron 9.0.0 shell.openItem API 已替换为异步 shell.openPath API.shell.openPath 文档
Note: Electron 9.0.0 The
shell.openItemAPI has been replaced with an asynchronousshell.openPathAPI. shell.openPath docs
这篇关于使用 NodeJS 和 Electron 使用操作系统的默认应用程序(带有 Word 的 docx 等)打开外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
discord.js v12:我如何等待 DM 频道中的消息?discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
如何让我的机器人提及发出该机器人命令的人how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
如何列出来自特定服务器的所有成员?How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)