主进程background.js
import { ipcMain, dialog } from 'electron';
ipcMain.handle('dialog:openDirectory', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog(win, {
properties: ['openDirectory']
})
if (canceled) {
return
} else {
return filePaths[0]
}
})
渲染进程
const path = await window?.ipcRenderer?.selectFolder();
preload.js
const { contextBridge: bridge, ipcRenderer } = require('electron');
bridge.exposeInMainWorld('ipcRenderer', {
selectFolder: () => ipcRenderer.invoke('dialog:openDirectory')
});