4

I am using System.Windows; and System.Windows.Controls; so I can't use System.Windows.Forms; because there is a lot of controls like messagebox and list box...etc are common between them is there another solution to get folderbrowserdialog without using System.Windows.Forms; or is there any get folder location dialog box ?

kartal
  • 17,436
  • 34
  • 100
  • 145

3 Answers3

5

You can use the FolderBrowserDialog; either explicitly place the namespace in front of the class...

System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();

...or create an alias with regard to your namespace.

Imports [ aliasname = ] namespace
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • I got this exception in browse.ShowDialog() the exception is Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. – kartal Jul 08 '11 at 22:01
  • @salamonti Looks like you got everything working; being you accepted the answer, let me know if you still need assistance. – Aaron McIver Jul 08 '11 at 23:53
  • Not sure why but your code above has FolderDialoespaceg(); It should be new System.Windows.Forms.FolderBrowserDialog(); – Victor Chelaru Jan 24 '12 at 19:39
  • 1
    The main problem with this approach is that it introduces the need for the System.Windows.Forms assembly reference which then invites other developers to start using WinForms features unintentionally such as MessageBox, etc. – jpierson Mar 25 '13 at 19:22
  • @jpierson is right, it's really not nice to use WinForms in a WPF project! – florien Nov 01 '19 at 23:33
5

I have also encountered this using FolderBrowserDialog in WPF with listBox.

because we use WPF, we need to add winform component "FolderBrowserDialog".

  • right click on the project name or reference, and choose "Add reference"
  • choose .Net tab and browse for System.Windows.Forms.

So now you can use FolderBrowserDialog in WPF.

private FolderBrowserDialog openFolder = new FolderBrowserDialog();

Here is a link to a post on my blog that contains the image and a short explanation http://syumulnetwork.blogspot.com/2011/09/myth-self-note-5-c-wpf.html

Verbeia
  • 4,400
  • 2
  • 23
  • 44
2

See my answer to Select folder dialog WPF for some example code. Basically the Windows Presentation Foundation 4.5 Cookbook recommends that you use the Windows® API Code Pack for Microsoft® .NET Framework if you need a folder browser.

Community
  • 1
  • 1
T Powers
  • 1,637
  • 3
  • 13
  • 11
  • It seems like this is the only answer on the internet which is not proposing the guilt including WinForms in a WPF project. You should probably update your answer to include some of its usage, something like this: using (Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog dbd = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog() { IsFolderPicker = true }) – florien Nov 01 '19 at 23:48