0

I"m a complete noob in Visual Studio. I'm trying to write a simple app using WPF. I need a dialog that would pick a folder. I know that WPF doesn't have one, and I need to use Windows.Forms with their FolderBrowserDialog. I need to add Widnows.Forms framework to references so that I could say using System.Windows.Forms; and then just do this dialog. However when I'm trying to follow the instructions, I do not see any Windows.Forms here, except for what's on the image. And even if I add those, it still doesn't take using System.Windows.Forms; What should I do? This is References MAnager for the project

miguello
  • 544
  • 5
  • 15
  • 1
    It looks like you built a .Net 5+ Project. Edit the Project file (right-click on the Project's name) and add `true` -- If you followed instructions related to a .Net Framework Project, you should have seen `References`, not `Dependencies` – Jimi Jun 21 '22 at 19:48
  • 1
    If you have a .Net 6 Project, you can also open up the Project's Properties and tick `Enable Windows Forms for this Project` (it's the same thing as the above) – Jimi Jun 21 '22 at 19:56
  • @Jimi I think I had long term support .Net 6 – miguello Jun 21 '22 at 20:31
  • 1
    Then the latter option, less manual labor required (you have to add `using System.Windows.Forms;` *manually* nonetheless). – Jimi Jun 21 '22 at 20:31
  • 1
    As a note, you also have these options: [How do I use OpenFileDialog to select a folder?](https://stackoverflow.com/q/11624298/7444103)-- Check [Simon Mourier's answer](https://stackoverflow.com/a/66187224/7444103) first. Also [OpenFileOrFolderDialog](https://github.com/scottwis/OpenFileOrFolderDialog) – Jimi Jun 21 '22 at 20:57

1 Answers1

1
  1. First create a .Net6 Wpf project
  2. Double-click the project to enter csproj and manually add the following code:
<UseWindowsForms>true</UseWindowsForms>

enter image description here

  1. Manually enter the following new folderbrowserdialog code, and then use intellisense to automatically add using System.Windows.Forms; That's it (you can also add it manually)
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

enter image description here

  1. call it
private void Button_Click(object sender, RoutedEventArgs e)
{
  using FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
  {
    folderBrowserDialog.ShowDialog();
  }
}
  1. You can also go to Property->Application->General->Windows Forms->Enable Windows Forms for this Project in the .net6 project.

enter image description here

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21