Skip to content
github-actions[bot] edited this page Oct 15, 2025 · 2 revisions

Display native system dialogs for opening and saving files, alerting, etc.

Overview

The Electron.Dialog API provides access to native system dialogs for file operations, message boxes, and certificate trust dialogs. These dialogs are modal and provide a consistent user experience across different platforms.

Key Methods

File Dialogs

ShowOpenDialogAsync

Parameters:

  • browserWindow (optional) - Parent window for modal behavior
  • options - Dialog configuration options

Returns: Array of selected file paths

ShowSaveDialogAsync

Parameters:

  • browserWindow (optional) - Parent window for modal behavior
  • options - Dialog configuration options

Returns: Selected file path or empty string if cancelled

Message Dialogs

ShowMessageBoxAsync

Parameters:

  • browserWindow (optional) - Parent window for modal behavior
  • message or messageBoxOptions - Message content and configuration

Returns: MessageBoxResult with response index and checkbox state

ShowErrorBox

Parameters:

  • title - Dialog title
  • content - Error message content

Certificate Dialogs

ShowCertificateTrustDialogAsync

Parameters:

  • browserWindow (optional) - Parent window for modal behavior
  • options - Certificate trust dialog options

Dialog Options

OpenDialogOptions

var options = new OpenDialogOptions
{
    Title = "Select Files",
    DefaultPath = "/home/user/documents",
    ButtonLabel = "Select",
    Properties = new[]
    {
        OpenDialogProperty.OpenFile,
        OpenDialogProperty.MultiSelections,
        OpenDialogProperty.CreateDirectory
    },
    Filters = new[]
    {
        new FileFilter { Name = "Images", Extensions = new[] { "jpg", "png", "gif" } },
        new FileFilter { Name = "All Files", Extensions = new[] { "*" } }
    }
};

SaveDialogOptions

var options = new SaveDialogOptions
{
    Title = "Save As",
    DefaultPath = "document.txt",
    ButtonLabel = "Save",
    Filters = new[]
    {
        new FileFilter { Name = "Text Files", Extensions = new[] { "txt" } }
    }
};

MessageBoxOptions

var options = new MessageBoxOptions
{
    Type = MessageBoxType.Info,
    Title = "Information",
    Message = "Operation completed successfully.",
    Buttons = new[] { "OK" },
    DefaultId = 0,
    CancelId = 0
};

Platform-Specific Behavior

Windows & Linux

  • File vs Directory Selection: Cannot show both file and directory selection simultaneously
  • Property Priority: If both openFile and openDirectory are set, directory selection takes precedence

macOS

  • Certificate Dialogs: Full certificate trust dialog support
  • File Type Handling: Enhanced file type filtering and preview

Usage Examples

File Operations

// Open multiple files
var files = await Electron.Dialog.ShowOpenDialogAsync(window, new OpenDialogOptions
{
    Properties = new[] { OpenDialogProperty.OpenFile, OpenDialogProperty.MultiSelections }
});

// Save with custom extension
var path = await Electron.Dialog.ShowSaveDialogAsync(window, new SaveDialogOptions
{
    DefaultPath = "backup.json",
    Filters = new[] { new FileFilter { Name = "JSON", Extensions = new[] { "json" } } }
});

User Confirmation

// Confirmation dialog
var result = await Electron.Dialog.ShowMessageBoxAsync(window, new MessageBoxOptions
{
    Type = MessageBoxType.Question,
    Title = "Confirm Delete",
    Message = $"Delete {filename}?",
    Buttons = new[] { "Cancel", "Delete" },
    DefaultId = 0,
    CancelId = 0
});

if (result.Response == 1)
{
    // Delete file
}

Error Handling

// Error dialog
Electron.Dialog.ShowErrorBox("Save Failed", "Could not save file. Please check permissions and try again.");

// Warning dialog
await Electron.Dialog.ShowMessageBoxAsync(new MessageBoxOptions
{
    Type = MessageBoxType.Warning,
    Title = "Warning",
    Message = "This operation may take several minutes.",
    Buttons = new[] { "Continue", "Cancel" }
});

Related APIs

Additional Resources

Clone this wiki locally