Electron 使用默认方式打开文件:完整指南和示例代码
在 Electron 中,您可以使用以下代码来使用默认方式打开文件:
const { shell } = require('electron')
shell.openItem('/path/to/file')
其中,'shell' 模块提供了许多与操作系统交互的功能,包括打开文件、打开外部链接等。'openItem()' 方法可以打开指定路径的文件或目录,使用操作系统默认的方式打开。
需要注意的是,'openItem()' 方法是异步的,因此可以使用回调函数来处理打开文件的结果:
shell.openItem('/path/to/file', (err) => {
if (err) {
console.error(err)
} else {
console.log('File opened successfully')
}
})
通过使用这些代码,您可以在 Electron 应用中轻松地打开文件,并使用默认应用程序处理它们。
原文地址: https://www.cveoy.top/t/topic/kWku 著作权归作者所有。请勿转载和采集!