0

我正在开发一个可以打开目录文件选择器并返回一个值的电子应用程序,就像这个问题是如何做到的:Electron - Open Folder Dialog

ipcMain.on('selectDirectory', function() {

    dir = dialog.showOpenDialog(mainWindow, {

        properties: ['openDirectory']

    });

});

但是我找不到打开文件夹对话框而不返回任何内容的方法,就像我作为用户想要打开文件的位置一样。我不想通过等待选定的文件/文件夹来锁定我的程序。我只想通过我的跨平台电子应用程序(mac/win/linux)在用户机器上打开一个特定位置的窗口

4

1 回答 1

0

Electrondialog.showOpenDialog()返回一个承诺,因此需要这样处理。

ipcMain.on('selectDirectory', function() {

    // Define the options.
    let options = {
        title: "My title",
        properties: ['openDirectory'],
        defaultPath: "/absolute/path/to/open/at"   // Optional.
    }

    // Show the open (folder) dialog.
    dialog.showOpenDialog(mainWindow, options)
        .then((result) => {
            // Bail early if user cancelled dialog.
            if (result.canceled) { return }

            // Get the selected path.
            let path = result.filePaths[0];

            // More processing...
        })
});

如果您想设置对话框打开的初始路径,请设置defaultPath属性。

path变量将为您提供用户在对话框中选择的路径。

于 2022-02-19T13:17:11.650 回答