3

I am having trouble with a new .NET 5.0 application. I want to have a select folder dialog open, but I haven't found a class for that. All the code I find references the System.Windows.Forms library.

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

What is the new way?

Alan
  • 2,046
  • 2
  • 20
  • 43
  • 2
    Why would there be a new way ? – Franck Nov 19 '20 at 16:20
  • 1
    @Franck because there is a new way. Windows 7 introduced completely new dialogs and people have been trying to use them since. There was no direct support even in Windows Forms for eg [the TaskDialog](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.taskdialog?view=net-5.0&viewFallbackFrom=netcore-3.1) before .NET Core 5, as Microsoft tried to push people to UWP and failed. Those classes are getting added now that both Windows Forms and WPF are open source – Panagiotis Kanavos Nov 19 '20 at 16:42
  • @PanagiotisKanavos interesting. Never heard of it before. It must not have been publicized enough. – Franck Nov 19 '20 at 18:38
  • while this is marked as duplicate, it addresses about using FolderBrowser and not including Windows.System.Forms so the answer might be same but questions are different. – Vishesh Mangla Jun 24 '23 at 06:45

1 Answers1

16

I didn't realize I needed to edit the .csproj file, nor did I know you could have both WPF and Windows Forms declared at the same time there. I kept thinking I needed to add it as a reference.

Modifying the project file in this way worked and allowed me to declare using System.Windows.Forms; without getting an error.

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <RootNamespace>WpfApp1_5</RootNamespace>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
Alan
  • 2,046
  • 2
  • 20
  • 43
  • You should check what *new* dialogs are available in .NET 5 - [the TaskDialog was added to .NET 5](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.taskdialog?view=net-5.0) after 11 years of waiting. I wouldn't be surprised if other "new" dialogs start appearing, or if WPF versions appear as well – Panagiotis Kanavos Nov 19 '20 at 16:43