---
title: "Open the Linux file chooser via the XDG portal in C#"
url: https://recursiveai.org/oneplatform/cookbook/show-native-linux-file-chooser-with-xdg-portal/
description: "Use OnePlatform’s FileChooserPortal to open a native Linux file dialog from a C# console app. Read the selected local paths or handle cancellation."
version: "1.1.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/show-native-linux-file-chooser-with-xdg-portal/show-native-linux-file-chooser-with-xdg-portal.zip)

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.

```shell
dotnet add package RecursiveAI.OnePlatform
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneplatform/cookbook/show-native-linux-file-chooser-with-xdg-portal/show-native-linux-file-chooser-with-xdg-portal.zip) 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:

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

## Related recipes

- [Create a native desktop window](https://recursiveai.org/oneplatform/cookbook/create-native-cross-platform-window/)
- [Read and write system clipboard text](https://recursiveai.org/oneplatform/cookbook/read-write-system-clipboard-text/)

## 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](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.FileChooser.html) 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](https://flatpak.github.io/xdg-desktop-portal/docs/portals.conf.html). 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.
