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.
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:
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:
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:
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
- '
OpenFileDialogis intended for files, not folders.' - Use
FolderBrowserDialogwhen you need a built-in folder picker. - Use
CommonOpenFileDialogwithIsFolderPicker = truefor a more modern Windows picker. - Use
OpenFileDialogonly 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
- How do you convert a string to a byte array in .NET?
- How do you count the lines of code in a Visual Studio solution?
- How do You Create a Read-Only Dependency Property?
- How do you debug MVC 4 API routes?
- How do you do a deep copy of an object in .NET?
- How do you do Impersonation in .NET?
- How do you enable Enable .NET Framework source stepping?
- How do you format code in Visual Studio Code (VSCode)?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.