Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select folder dialog WPF

I develop a WPF4 application and in my app I need to let the user select a folder where the application will store something (files, generated reports etc.).

My requirements:

  • Ability to see the standard folder tree

  • Ability to select a folder

  • WPF look & feel, this dialog must look like part of a modern application designed for Windows Vista/7 and not Windows 2000 or even Win9x.

As I understand, until 2010 (.Net 4.0) there won't be a standard folder dialog, but maybe there are some changes in version 4.0?

Or all what rest to do is use old-school WinForms dialog? If it's the only way to do what I need, how can I make it closer to Vista/7 style and not Win9x?

On some forums, I saw the implementation of such dialogs but with old ugly icons à la Windows 95. It really doesn't look nice.

like image 364
Mike Avatar asked Oct 24 '10 10:10

Mike


People also ask

How do I use FolderBrowserDialog?

Unlike other Windows Forms controls, a FolderBrowserDialog does not have or need visual properties like others. To create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio.

What is the meaning of WPF?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.


1 Answers

Windows Presentation Foundation 4.5 Cookbook by Pavel Yosifovich on page 155 in the section on "Using the common dialog boxes" says:

"What about folder selection (instead of files)? The WPF OpenFileDialog does not support that. One solution is to use Windows Forms' FolderBrowseDialog class. Another good solution is to use the Windows API Code Pack described shortly."

I downloaded the API Code Pack from Windows® API Code Pack for Microsoft® .NET Framework Windows API Code Pack: Where is it?, then added references to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll to my WPF 4.5 project.

Example:

using Microsoft.WindowsAPICodePack.Dialogs;  var dlg = new CommonOpenFileDialog(); dlg.Title = "My Title"; dlg.IsFolderPicker = true; dlg.InitialDirectory = currentDirectory;  dlg.AddToMostRecentlyUsedList = false; dlg.AllowNonFileSystemItems = false; dlg.DefaultDirectory = currentDirectory; dlg.EnsureFileExists = true; dlg.EnsurePathExists = true; dlg.EnsureReadOnly = false; dlg.EnsureValidNames = true; dlg.Multiselect = false; dlg.ShowPlacesList = true;  if (dlg.ShowDialog() == CommonFileDialogResult.Ok)  {   var folder = dlg.FileName;   // Do something with selected folder string } 
like image 101
T Powers Avatar answered Oct 16 '22 03:10

T Powers