Click here to Skip to main content
12,634,982 members (29,804 online)
Click here to Skip to main content
Add your own
alternative version

Stats

105.5K views
8.7K downloads
28 bookmarked
Posted

Select file or folder from the same dialog

, 30 Nov 2009 GPL3
Rate this:
Please Sign up or sign in to vote.
A relatively straightforward method to give users the option to select either a file or folder using the same dialog, in managed code.

FileFolderDialog

Introduction

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.

Background

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.

History

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

License

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

Share

About the Author

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

You may also be interested in...

Pro

Comments and Discussions

 
QuestionThanks bro Pin
Member 1190684613-Aug-15 4:46
memberMember 1190684613-Aug-15 4:46 
SuggestionVery mysterious article Pin
Member 112772596-Aug-15 2:24
memberMember 112772596-Aug-15 2:24 
QuestionDoesn't work on Windows 8.1 Pin
MartinNohrRimage12-Jun-15 12:00
memberMartinNohrRimage12-Jun-15 12:00 
AnswerRe: Doesn't work on Windows 8.1 Pin
Member 1084522918-Dec-15 5:31
memberMember 1084522918-Dec-15 5:31 
QuestionNubom Pin
nubom9-Jan-14 23:22
membernubom9-Jan-14 23:22 
AnswerRe: Nubom Pin
stankovski10-Jan-14 7:06
memberstankovski10-Jan-14 7:06 
QuestionDo you have this code in VB.NET? Pin
Member 1045754310-Dec-13 5:58
memberMember 1045754310-Dec-13 5:58 
QuestionThanks, that was exactlly what I have needed Pin
ben27531-Oct-13 5:38
memberben27531-Oct-13 5:38 
QuestionFolder can not be selected Pin
sunnoheem22-Oct-13 6:41
membersunnoheem22-Oct-13 6:41 
AnswerRe: Folder can not be selected Pin
stankovski10-Jan-14 7:09
memberstankovski10-Jan-14 7:09 
QuestionLead to Gold Pin
MilesAhead28-Nov-12 7:34
memberMilesAhead28-Nov-12 7:34 
QuestionNot able to select folder on Win7 and VS 2008 Pin
Member 325620514-Sep-11 0:37
memberMember 325620514-Sep-11 0:37 
NewsDoesn't work Pin
Member 418359028-Jan-11 14:01
memberMember 418359028-Jan-11 14:01 
GeneralGood but short Pin
Toli Cuturicu19-Aug-10 6:08
memberToli Cuturicu19-Aug-10 6:08 
Questioncompatibility issues? Pin
IGood13-Dec-09 17:37
memberIGood13-Dec-09 17:37 
AnswerRe: compatibility issues? Pin
stankovski22-Dec-09 4:44
memberstankovski22-Dec-09 4:44 
GeneralRe: compatibility issues? Pin
IGood2-Jan-10 13:44
memberIGood2-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
memberNayan 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 
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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 30 Nov 2009
Article Copyright 2009 by stankovski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid