package.json中配置
"fileAssociations": [
{
"ext": "", // 后缀名 不需要加.
"role": "Editor"
}
]
创建ts
import { ipcMain } from 'electron'
import path from 'path'
import fs from 'fs'
export function onGetOpenFileData(process: any) {
ipcMain.handle('getOpenFileData', () => {
if (process && process.argv) {
const argv = process.argv
// 判断打开参数中是否有需要的文件
const file = argv.find((c: any) => c.indexOf('.yr') !== -1)
if (file) {
return {
filename: path.basename(file),
data: fs.readFileSync(file, 'utf-8')
}
}
}
return null
})
}
在应用中 使用获取打开文件
window?.ipc?.invoke('getOpenFileData')