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


32 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)

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

Comments and Discussions

QuestionThank you very much Pin
matsom114-Dec-20 0:43
Membermatsom114-Dec-20 0:43 
SuggestionMinor suggestion Pin
Mailgoatz14-Sep-18 4:54
MemberMailgoatz14-Sep-18 4:54 
PraiseThe returned path appended "\Folder Selection" Pin
get_junaidu26-Feb-18 11:59
Memberget_junaidu26-Feb-18 11:59 
GeneralMy vote of 5 Pin
Froopy8-Apr-17 16:01
MemberFroopy8-Apr-17 16:01 
GeneralDoesn't "really" work Pin
Mysteryx9331-Jan-17 19:48
MemberMysteryx9331-Jan-17 19:48 
QuestionThanks bro Pin
Member 1190684613-Aug-15 3:46
MemberMember 1190684613-Aug-15 3:46 
SuggestionVery mysterious article Pin
Member 112772596-Aug-15 1:24
MemberMember 112772596-Aug-15 1:24 
QuestionDoesn't work on Windows 8.1 Pin
MartinNohrRimage12-Jun-15 11:00
MemberMartinNohrRimage12-Jun-15 11:00 
AnswerRe: Doesn't work on Windows 8.1 Pin
Member 1084522918-Dec-15 4:31
MemberMember 1084522918-Dec-15 4:31 
QuestionNubom Pin
nubom9-Jan-14 22:22
Membernubom9-Jan-14 22:22 
AnswerRe: Nubom Pin
stankovski10-Jan-14 6:06
Memberstankovski10-Jan-14 6:06 
QuestionDo you have this code in VB.NET? Pin
juno16810-Dec-13 4:58
Memberjuno16810-Dec-13 4:58 
QuestionThanks, that was exactlly what I have needed Pin
ben27531-Oct-13 4:38
Memberben27531-Oct-13 4:38 
QuestionFolder can not be selected Pin
sunnoheem22-Oct-13 5:41
Membersunnoheem22-Oct-13 5:41 
AnswerRe: Folder can not be selected Pin
stankovski10-Jan-14 6:09
Memberstankovski10-Jan-14 6:09 
QuestionLead to Gold Pin
MilesAhead28-Nov-12 6:34
MemberMilesAhead28-Nov-12 6:34 
QuestionNot able to select folder on Win7 and VS 2008 Pin
Member 325620513-Sep-11 23:37
MemberMember 325620513-Sep-11 23:37 
NewsDoesn't work PinPopular
Member 418359028-Jan-11 13:01
MemberMember 418359028-Jan-11 13:01 
GeneralGood but short Pin
Toli Cuturicu19-Aug-10 5:08
MemberToli Cuturicu19-Aug-10 5:08 
Questioncompatibility issues? Pin
Ian Good13-Dec-09 16:37
MemberIan Good13-Dec-09 16:37 
AnswerRe: compatibility issues? Pin
stankovski22-Dec-09 3:44
Memberstankovski22-Dec-09 3:44 
GeneralRe: compatibility issues? Pin
Ian Good2-Jan-10 12:44
MemberIan Good2-Jan-10 12:44 
GeneralRe: compatibility issues? Pin
Riz Thon5-Jan-10 20:14
MemberRiz Thon5-Jan-10 20:14 
AnswerRe: compatibility issues? Pin
Riz Thon5-Jan-10 20:19
MemberRiz Thon5-Jan-10 20:19 
GeneralRe: compatibility issues? Pin
Nayan Choudhary21-Sep-10 0:55
professionalNayan Choudhary21-Sep-10 0:55 

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.