How do I use OpenFileDialog to select a folder?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
OpenFileDialog is designed for files, not folders. If your real goal is folder selection, the cleanest answer is usually "do not use OpenFileDialog for that". In WinForms, the normal choice is FolderBrowserDialog, and in some modern Windows-only cases you may prefer a more advanced folder picker. The OpenFileDialog workaround exists, but it is still a workaround.
Use FolderBrowserDialog When You Actually Need a Folder
For classic WinForms code, FolderBrowserDialog is the direct tool for the job.
That is simpler, clearer, and less fragile than repurposing a file dialog.
The OpenFileDialog Workaround Still Exists
Sometimes you are constrained to OpenFileDialog, usually because of UI consistency or legacy code. In that case, the common trick is to disable file-name validation and provide a dummy file name.
This works because the dialog lets the user navigate to a folder and "select" a fake file name inside it. You then strip off the fake name and keep the directory path.
Know the Tradeoff
The workaround is acceptable when you understand what it is doing, but it is not the same as native folder selection behavior.
What you gain:
- reuse of the familiar file-dialog UI
- sometimes a more modern-looking shell dialog than
FolderBrowserDialog
What you lose:
- semantic clarity
- a true folder-selection API
- some predictability around edge cases and UX details
That is why the best answer is usually still FolderBrowserDialog unless you have a concrete reason to avoid it.
Keep the User Experience Clean
No matter which dialog you choose, a few details matter:
- set a clear description or title
- initialize the dialog to a sensible starting directory
- handle cancel cleanly
- store the last selected path if the workflow repeats
Example with an initial folder:
Those small touches improve usability more than arguing about the dialog class name.
If You Need a More Modern Folder Picker
Some applications use newer Windows shell APIs or helper libraries for a more modern folder-picking experience. That can be a good choice in Windows-only desktop apps, but it is separate from the question of whether OpenFileDialog itself supports folders. By default, it does not.
So the practical answer remains:
- use the dedicated folder dialog when possible
- use the
OpenFileDialogtrick only when you must
Common Pitfalls
- Assuming
OpenFileDialoghas native folder-selection support. - Using the workaround without understanding why the fake file name is needed.
- Forgetting to call
Path.GetDirectoryNameand accidentally keeping the placeholder file name. - Choosing
OpenFileDialogjust because it looks nicer, even whenFolderBrowserDialogis a better semantic fit. - Not handling cancel and null-path cases cleanly.
Summary
- '
OpenFileDialogis for files, not folders.' - For WinForms folder selection,
FolderBrowserDialogis usually the correct choice. - An
OpenFileDialogworkaround is possible withValidateNames = falseand a dummy file name. - The workaround is useful in legacy or UI-constrained cases, but it is still a workaround.
- Choose the dialog that matches the user action instead of forcing a file dialog into folder semantics.
Related reading
- How do I use reflection to determine the nested type element type of an array?
- How do I use WPF bindings with RelativeSource?
- How do I verify a method was called exactly once with Moq?
- How do I view the SQL generated by the Entity Framework?
- how do I work around log4net keeping changing publickeytoken
- How do I write LINQ's .Skip1000.Take100 in pure SQL?
- How do I write one to many query in Dapper.Net?
- How do the semantics of AsyncLocal differ from the logical call context?

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.