Click here to Skip to main content
Licence GPL3
First Posted 25 Nov 2009
Views 18,645
Downloads 844
Bookmarked 19 times

Select file or folder from the same dialog

By | 30 Nov 2009 | Article
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)

About the Author

stankovski

Team Leader

United States United States

Member

Denis Stankovski is a software development lead in a consulting company.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNot able to select folder on Win7 and VS 2008 PinmemberMember 325620523:37 13 Sep '11  
NewsDoesn't work PinmemberMember 418359013:01 28 Jan '11  
GeneralGood but short PinmemberToli Cuturicu5:08 19 Aug '10  
Questioncompatibility issues? PinmemberIGood16:37 13 Dec '09  
AnswerRe: compatibility issues? Pinmemberstankovski3:44 22 Dec '09  
GeneralRe: compatibility issues? PinmemberIGood12:44 2 Jan '10  
GeneralRe: compatibility issues? PinmemberRiz Thon20:14 5 Jan '10  
AnswerRe: compatibility issues? PinmemberRiz Thon20:19 5 Jan '10  
GeneralRe: compatibility issues? PinmemberNayan Choudhary0:55 21 Sep '10  
QuestionHow to get multiple selected folder and file path PinmemberPatel Pranav20:26 30 Nov '09  
AnswerRe: How to get multiple selected folder and file path Pinmemberstankovski4:00 1 Dec '09  
GeneralFilename "Folder Selection." PinmemberAndromeda Shun21:33 25 Nov '09  
GeneralRe: Filename "Folder Selection." Pinmemberstankovski12:30 29 Nov '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 30 Nov 2009
Article Copyright 2009 by stankovski
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid