48

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
david
  • 726
  • 1
  • 5
  • 10

2 Answers2

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    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?) – david Dec 28 '10 at 16:10
  • 6
    @David: As I said, _You need to add a reference to System.Windows.Forms.dll_. – SLaks Dec 28 '10 at 16:13
  • unfortunately it gives me the same error. – david Dec 28 '10 at 16:19
  • 2
    @david: That means that you didn't add a reference. Right-click the project and click Add Reference. – SLaks Dec 28 '10 at 16:19
  • 2
    _using WinForms = System.Windows.Forms;_ really helped me to avoid ambiguous references. Thank you! – mandarin Oct 01 '14 at 09:57
  • 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. – guan boshen Sep 19 '19 at 14:01
25

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

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 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. Thanks – david Dec 28 '10 at 16:15
  • this should work - as SLaks pointed out you need to add a reference to System.Windows.Forms.dll in your project – BrokenGlass Dec 28 '10 at 16:20
  • 2
    you should also check the result. Eg: if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = dialog.SelectedPath; } – Chielus Jan 04 '12 at 15:44
  • Nothing in the referenced so actually works (classes missing from references) but this works perfectly for me. – Nick.Mc Jun 17 '17 at 10:16