---
title: "Open a native file dialog in a C# desktop app"
url: https://recursiveai.org/oneapp/cookbook/open-native-file-picker/
description: "Show the system’s open file dialog from a C# app. Filter for text files, handle Cancel, and use the selected paths in OneApp."
version: "1.0.0"
---

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.

```shell
dotnet add package RecursiveAI.OneApp
```

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

```cs
// 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](https://recursiveai.org/oneapp/cookbook/open-native-file-picker/open-native-file-picker.zip) 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.`

```csharp
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:

```shell
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.

## Related recipes

- [Build a desktop app with HTML and CSS in C#](https://recursiveai.org/oneapp/cookbook/build-desktop-app-with-html-css/)
- [Automate UI tests for a C# desktop app](https://recursiveai.org/oneapp/cookbook/drive-desktop-app-from-tests/)
- [Show desktop notifications from a C# tray app](https://recursiveai.org/oneapp/cookbook/show-desktop-notification-from-tray-app/)


## 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](https://recursiveai.org/oneapp/docs/shell/#desktop-services-hostshell) 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.
