With the showOpenDialog you can open files or folders. To show an open dialog all you need to do is:
const { dialog } = require('electron')
const selectedPaths = dialog.showOpenDialog();
console.log(selectedPaths);
the showOpenDialog returns an array of strings with the paths chosen by the user. If you instead decide to use the callback method it will return undefined
.
There are more options to showOpenDialog to use. Take a look at the docs to se what the options are.
In the electron-tutorial-app this code is used to display a showOpenDialog. It also uses ipc to communicate between the main and renderer process:
const { ipcMain, dialog } = require('electron')
ipcMain.on('show-open-dialog', (event, arg)=> {
const options = {
//title: 'Open a file or folder',
//defaultPath: '/path/to/something/',
//buttonLabel: 'Do it',
/*filters: [
{ name: 'xml', extensions: ['xml'] }
],*/
//properties: ['showHiddenFiles'],
//message: 'This message will only be shown on macOS'
};
dialog.showOpenDialog(null, options, (filePaths) => {
event.sender.send('open-dialog-paths-selected', filePaths)
});
})
As you can see above some ipc-stuff is used. Take a look at the dialog.js to see how the message is sent and how the response is handled.