OpenFileDialog
folder selection
configuration guide
Windows Forms
C# programming

How do you configure an OpenFileDialog to select folders?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

OpenFileDialog is built for files, not directories, so there is no supported setting that turns it into a real folder picker. If the user needs to choose a folder, the correct solution is usually to switch dialogs instead of trying to force file-picker behavior into a directory-selection workflow.

Use a Folder Dialog Instead

In Windows Forms, the classic choice is FolderBrowserDialog:

csharp
1using System.Windows.Forms;
2
3public static string? PickFolder(IWin32Window owner)
4{
5    using var dialog = new FolderBrowserDialog
6    {
7        Description = "Select an output folder",
8        ShowNewFolderButton = true
9    };
10
11    return dialog.ShowDialog(owner) == DialogResult.OK
12        ? dialog.SelectedPath
13        : null;
14}

This dialog is designed for directories, so the return value is a folder path and the user experience matches the task.

That is the important point: when the requirement is "choose a folder," the best configuration for OpenFileDialog is usually not to use OpenFileDialog at all.

Why OpenFileDialog Is the Wrong Tool

OpenFileDialog is file-centric in multiple ways:

  • it validates files
  • it exposes file filters and extensions
  • it returns a chosen file path
  • its UI suggests file selection, not directory selection

There have been workarounds over the years, but they are fragile because they depend on behavior the dialog was not meant to guarantee. Even if a hack appears to work on one Windows version, it can break validation, confuse users, or behave differently on another machine.

A More Modern Folder Picker

If FolderBrowserDialog feels too old-fashioned, many .NET desktop apps use CommonOpenFileDialog from the Windows API Code Pack:

csharp
1using Microsoft.WindowsAPICodePack.Dialogs;
2
3public static string? PickFolderModern()
4{
5    using var dialog = new CommonOpenFileDialog
6    {
7        IsFolderPicker = true
8    };
9
10    return dialog.ShowDialog() == CommonFileDialogResult.Ok
11        ? dialog.FileName
12        : null;
13}

This gives you a newer shell-style picker while still supporting directory selection directly. It is often a better fit when UX matters and adding a small dependency is acceptable.

WPF and Mixed Desktop Applications

WPF does not include a built-in folder picker as convenient as Windows Forms. Because of that, many WPF applications still reference System.Windows.Forms just for FolderBrowserDialog. That is a practical solution, especially in internal tools.

If the application uses MVVM or a layered architecture, it is usually cleaner to wrap the picker in a small service interface so the UI framework choice stays isolated.

If You Actually Need the Folder of a Chosen File

Sometimes the requirement is not folder selection at all. Sometimes the user should pick a file, and your code only needs the containing directory afterward. In that case, OpenFileDialog is fine:

csharp
1using System.IO;
2using System.Windows.Forms;
3
4public static string? PickFileThenGetFolder()
5{
6    using var dialog = new OpenFileDialog();
7
8    if (dialog.ShowDialog() != DialogResult.OK)
9        return null;
10
11    return Path.GetDirectoryName(dialog.FileName);
12}

That is a different workflow, and it is worth being precise about the difference when you design the feature.

Common Pitfalls

The biggest mistake is searching for a hidden OpenFileDialog property that enables folder mode. There is no supported first-class switch for that behavior.

Another common issue is using a hack that technically returns a folder path but still shows file-oriented UI. Even when the code works, the user experience is misleading.

It is also easy to choose FolderBrowserDialog and later dislike the older shell appearance. If that matters, switch to a modern folder-picker library rather than bending OpenFileDialog.

Summary

  • 'OpenFileDialog is intended for files, not folders.'
  • Use FolderBrowserDialog when you need a built-in folder picker.
  • Use CommonOpenFileDialog with IsFolderPicker = true for a more modern Windows picker.
  • Use OpenFileDialog only when file selection is the real task and the directory is derived afterward.
  • Choosing the right dialog is more reliable than trying to hack file-dialog behavior into folder selection.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.