Click here to Skip to main content
15,123,509 members
Articles / Desktop Programming / Windows Forms
Posted 25 Nov 2009


31 bookmarked

Select file or folder from the same dialog

Rate me:
Please Sign up or sign in to vote.
4.68/5 (17 votes)
30 Nov 2009GPL31 min read
A relatively straightforward method to give users the option to select either a file or folder using the same dialog, in managed code.



This code demonstrates a way to use the managed FileOpenDialog control (available in .NET 2.0+) to open either a file or a folder from within the same dialog.


For my Open-Source project, I was looking for a control that would allow users to select either a file or a folder from within the same dialog. Having searched the web, I came across a few solutions (one involving calling non-managed code, and one involving a custom-written control). Unfortunately, both solutions didn't work for me. The first one used the FolderSelect dialog (which is pretty inconvenient compared to the FileOpen dialog), and the second one did not provide 100% of the Windows dialog functionality.

I then decided to look into the source code of the WinMerge utility to see how they implemented similar functionality in C++ (and potentially used some native calls). It turned out that the solution was available right inside the .NET framework.

Using the code

The key to getting OpenFileDialog to select both files and folders is to set the ValidateNames and CheckFileExists properties to false (dialog.ValidateNames = false; dialog.CheckFileExists = false) and set FileName to some special keyword to make sure that folders get selected (dialog.FileName = "Folder Selection";).

// Set validate names and check file exists to false otherwise windows will
// not let you select "Folder Selection."
dialog.ValidateNames = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = true;


// Always default to Folder Selection.
dialog.FileName = "Folder Selection.";

For convenience, I have wrapped OpenFileDialog inside a custom class, FileFolderDialog, and have also added a few helper properties to parse the returned file name.


  • 11/25/2009 - First version of the article published.
  • 11/30/2009 - Updated code to work with Windows 7.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Team Leader
United States United States
Denis Stankovski is a software development lead in a consulting company.

Comments and Discussions

AnswerRe: compatibility issues? Pin
stankovski22-Dec-09 4:44
Memberstankovski22-Dec-09 4:44 
GeneralRe: compatibility issues? Pin
Ian Good2-Jan-10 13:44
MemberIan Good2-Jan-10 13:44 
GeneralRe: compatibility issues? Pin
Riz Thon5-Jan-10 21:14
MemberRiz Thon5-Jan-10 21:14 
AnswerRe: compatibility issues? Pin
Riz Thon5-Jan-10 21:19
MemberRiz Thon5-Jan-10 21:19 
GeneralRe: compatibility issues? Pin
Nayan Choudhary21-Sep-10 1:55
professionalNayan Choudhary21-Sep-10 1:55 
QuestionHow to get multiple selected folder and file path Pin
Patel Pranav30-Nov-09 21:26
MemberPatel Pranav30-Nov-09 21:26 
AnswerRe: How to get multiple selected folder and file path Pin
stankovski1-Dec-09 5:00
Memberstankovski1-Dec-09 5:00 
GeneralFilename "Folder Selection." Pin
Andromeda Shun25-Nov-09 22:33
MemberAndromeda Shun25-Nov-09 22:33 

I haven't looked at your source code, but one thing struck me: What happens if the user selects a file named "Folder Selection."?

For opening existing files a check for existence of the file can distinguish between a file and a folder. But when the user can select either a file or a folder (then a default filename could be selected, and the folder is created if it doesn't exist). In this case a check for existence doesn't help.

Do you have any idea how to distinguish between a file or a folder selection in this case?

CU, Shun
GeneralRe: Filename "Folder Selection." Pin
stankovski29-Nov-09 13:30
Memberstankovski29-Nov-09 13:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.