Recursive AI

Open the Linux file chooser via the XDG portal in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Open the desktop’s own file dialog from a C# console app. Run it in a Linux graphical session with D-Bus (the desktop’s message bus) and a portal backend that provides the file dialog.

Terminal
dotnet add package RecursiveAI.OnePlatform

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

C#
using OnePlatform.DBus;
using OnePlatform.Shell;
 
using var bus = DBusConnection.ConnectSession();
var chooser = new FileChooserPortal(bus);
var result = await chooser.OpenFileAsync(new()
{
    Title = "Choose a file",
    CurrentFolder = Environment.CurrentDirectory
});
 
if (result.Cancelled)
    Console.WriteLine("No file selected.");
else
    foreach (var path in result.Paths)
    {
        var name = Path.GetFileName(path);
        Console.WriteLine($"Selected: {name}");
    }

If the desktop portal or its FileChooser interface is missing, OpenFileAsync throws DBusServiceUnavailableException.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Run from the unzipped example folder and choose the included sample.txt, or choose any local file if you pasted the code. Selecting it printed:

Text
Selected: sample.txt

Variations

To pick a directory instead, replace the OpenFileAsync call:

C#
var result = await chooser.OpenFolderAsync(new()
{
    Title = "Choose a folder",
    CurrentFolder = Environment.CurrentDirectory
});

The short answer

Connect to the D-Bus session bus with DBusConnection.ConnectSession(). Create a FileChooserPortal on that connection and await OpenFileAsync. Handle Cancelled before reading the result. Paths gives local paths; Uris gives the original file:// URIs from the portal. The dialog selects locations; it doesn’t open or read the files for you.

How does the XDG Desktop Portal file chooser work on Linux?

The FileChooser portal exposes a D-Bus interface. Your code requests a dialog, then awaits a response after the user picks something or dismisses it. The portal backend draws the dialog. You don’t need a PlatformApp or any rendering code in the console example. Keep the bus connection alive until the request completes.

When opening a dialog from an existing GUI window, FileChooserOptions.ParentWindow accepts a portal window identifier: an X11 window ID in x11:<hex xid> form, or an exported Wayland handle in wayland:<handle> form. The example leaves it blank because it has no parent. If fine-grained cancellation matters in your app, distinguish clicking Cancel (result.Cancelled) from cancelling the call with a .NET cancellation token (OperationCanceledException).

Why isn’t the XDG portal file chooser opening?

Check whether the app is running under the desktop’s session bus, and whether the session has both xdg-desktop-portal and a backend that exports FileChooser. A reachable session bus alone isn’t enough. Linux desktops can select different backends; see the portal’s backend configuration docs. Starting the same executable in a console outside the graphical session doesn’t guarantee a dialog can appear there.

OnePlatform uses the same portal for choosing a folder or asking where to save a file. These are separate calls: use OpenFolderAsync for a folder, and SaveFileAsync to ask for a destination. Choosing a save location doesn’t write a file.

All oneplatform recipes