Using FolderBrowserDialog in WPF application [duplicate]
Asked Answered
P

2

49

I have a WPF application that I need to have users access directories in. I have searched to the end of the world on how to integrate windows forms into WPF and have found all kinds of information on how to integrate form controls into my xaml, however, integrating a FolderBrowserDialog.

I am veteran programmer, but very new to .net (2nd day in fact), and I believe I can not find good information on immplementing this simply because I can not determine what the name/type is for a FolderBrowserDialog.

Oh, and I am using c# and Visual Studio 2008

Pangaro answered 28/12, 2010 at 15:58 Comment(1)
As of .NET 8, WPF now has support for a Folder Dialog.Camala
O
81

You need to add a reference to System.Windows.Forms.dll, then use the System.Windows.Forms.FolderBrowserDialog class.

Adding using WinForms = System.Windows.Forms; will be helpful.

Optometry answered 28/12, 2010 at 16:8 Comment(6)
I have tried this unfortunately, I get this error: Error 1 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)Pangaro
@David: As I said, You need to add a reference to System.Windows.Forms.dll.Optometry
unfortunately it gives me the same error.Pangaro
@david: That means that you didn't add a reference. Right-click the project and click Add Reference.Optometry
using WinForms = System.Windows.Forms; really helped me to avoid ambiguous references. Thank you!Susuable
We have been experiencing application crash using System.Windows.Forms.FolderBrowserDialog in WPF when logging-in with remote desktop (dotNet 4.5.1). Only crashes in Release version. Remove FolderBrowserDialog (do not call its functions) the application runs without any problem.Shirt
N
26

If I'm not mistaken you're looking for the FolderBrowserDialog (hence the naming):

var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();

Also see this SO thread: Open directory dialog

Negativism answered 28/12, 2010 at 16:7 Comment(4)
using the var as the type produces an error for me in C#, and replacing it with FolderBrowserDialog as I have seen done elsewhere produces a missing directive error, even with a System.Windows.Forms.FolderBrowserDialog directive. Is there something else I am doing wrong? I will check out that link immediately. ThanksPangaro
this should work - as SLaks pointed out you need to add a reference to System.Windows.Forms.dll in your projectNegativism
you should also check the result. Eg: if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = dialog.SelectedPath; }Lollipop
Nothing in the referenced so actually works (classes missing from references) but this works perfectly for me.Alpenstock

© 2022 - 2024 — McMap. All rights reserved.