Recursive AI

Open a native file dialog in a C# desktop app

OneApp is a free, open-source framework for desktop apps in .NET whose UI is HTML and CSS, drawn natively with no web view, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Open the system’s file picker on Windows, macOS, or Linux. On Linux, the desktop must provide the xdg-desktop-portal FileChooser service, as most GNOME and KDE Plasma desktops do.

Terminal
dotnet add package RecursiveAI.OneApp

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
// app: created by AppHost.Create in the full program
var shell = app.Shell;
var paths = await shell.OpenFilesAsync();
if (paths.Count == 0)
    Console.WriteLine("No file selected.");
foreach (var path in paths)
    Console.WriteLine(path);

The example is the full program below: unzip it and dotnet run.

Full program

The project opens the dialog at startup and looks in the current folder for *.txt. Run it from the unzipped folder and pick sample.txt, which contains one line: A sample to select in the file picker.

C#
using OneApp;
using OnePlatform.DBus;
using OnePlatform.Shell;
 
using var app = AppHost.Create(new("<h1>File picker</h1>")
{
    AppId = "org.example.FilePicker",
    Title = "File picker",
    Width = 400,
    Height = 180
});
app.Post(async () =>
{
    try
    {
        var paths = await app.Shell.OpenFilesAsync(new()
        {
            Title = "Choose a text file",
            Folder = Environment.CurrentDirectory,
            Filters = [("Text files", ["*.txt"])],
            Multiple = args.Contains("--multiple")
        });
        if (paths.Count == 0)
            Console.WriteLine("No file selected.");
        foreach (var path in paths)
            Console.WriteLine($"Selected: {Path.GetFileName(path)}");
    }
    catch (Exception error) when (error is DBusServiceUnavailableException
        or ShellUnavailableException)
    {
        Console.Error.WriteLine(error.Message);
        Environment.ExitCode = 1;
    }
    finally
    {
        app.Quit();
    }
});
app.Run();

The full example prints just the file name; paths contains the full local paths.

Text
Selected: sample.txt

Click Cancel instead and it prints:

Text
No file selected.

On Linux, if the session has no FileChooser portal, OpenFilesAsync throws DBusServiceUnavailableException; that’s a setup error, not a canceled selection.

Variations

Select more than one file. Run the same example with:

Terminal
dotnet run -- --multiple

This sets FileDialogOptions.Multiple to true; the foreach handles any number of returned paths. Copy sample.txt to a second .txt file in the same folder to try it.

The short answer

Await app.Shell.OpenFilesAsync from your OneApp app. Let the desktop do the browsing, then use the returned paths to open the files. An empty list means the user didn’t pick a file: it’s not an error and you shouldn’t try to read the first entry. Even a dialog that allows only one choice returns a list.

How can I open a file dialog in C#?

The dialog should run while the app’s event loop is active. The example posts the request before calling Run(), so the loop starts the request. In an editor you’d typically make the request when the user chooses File → Open, so they can dismiss it without interrupting startup. Await the task rather than blocking the event loop on .Result.

Choose a title that says what the user is picking. Folder can provide a useful starting point, and Filters takes display names paired with glob patterns. The example uses *.txt, an extension pattern that also fits the documented macOS filter requirements. A filter makes finding a file easier; it doesn’t parse or validate its contents. Keep selection separate from reading so you can handle an unreadable file independently.

What should happen when the user cancels an open file dialog?

In a real editor, keep showing the file that was already open. A cancellation returns no paths, so you can leave the existing state as it was. Passing a CancellationToken and canceling it in code is different: the task is canceled. The desktop shell docs describe which systems can close the visible dialog when the token is canceled.

The API stays the same while the desktop chooses the dialog: on Linux OpenFilesAsync uses the FileChooser portal; macOS uses NSOpenPanel. The pinned implementation passes a parent window handle only on Windows, so don’t rely on this version to tie the dialog to your main window on Linux or macOS.

All oneapp recipes