Using Folder Browser in WPF .NET 6.0

1k views Asked by At

I have been trying to use a folder browser dialog from System.Windows.Forms in .NET 6.0 WPF app, and every time I add the reference, it breaks my build and causes all kinds of wierd errors.

Has anyone figured this out?

I added a reference to System.Windows.Forms.dll by browsing to it, and it completely broke my build the second I added it.

4

There are 4 answers

0
Etienne de Martel On BEST ANSWER

Your issue is that you're referencing Windows Forms wrong.

As described in the documentation, to reference WinForms or WPF in a desktop project, starting with .NET 5, you only need to add properties to your .csproj, and MSBuild will do the rest:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework> <!-- or any other SDK -->

    <UseWPF>true</UseWPF> <!-- for WPF -->
    <UseWindowsForms>true</UseWindowsForms> <!-- for WinForms -->
  </PropertyGroup>

   <!-- other things go here -->

</Project>

In your case, which is a WPF project that uses some types from WinForms, you'd put both.

0
John On

I have a pretty simple workaround using a SaveFileDialog in case anyone needs it.

  using Microsoft.Win32;
  
  SaveFileDialog sfd = new();
  sfd.RestoreDirectory = true;
  sfd.Title = "Select Folder";
  sfd.FileName = "Select Folder and press Save";

  if (sfd.ShowDialog(this).Value)
  {
      txtTargetFolder.Text = System.IO.Path.GetDirectoryName(sfd.FileName);
  }
0
Neil Gim On

My recommendation is to not use WinForms, unless you don't want to install 3rd party NuGet packages. Instead, I suggest installing the Ookii.Dialogs.Wpf package. After that, you can replace your references to the WinForms FolderBrowserDialog with VistaFolderBrowserDialog. For example:

VistaFolderBrowserDialog folderSelector = new() {
    Description = "Description",
    Multiselect = false,
    ShowNewFolderButton = false,
    UseDescriptionForTitle = true
};

bool? result = folderSelector.ShowDialog(parentWindow);
if (result == true) {
    // The OK button was clicked.
} else if (result == false) {
    // The folder selector was closed without clicking the OK button.
} else if (result == null) {
    // Documentation doesn't say when it would return Null. My guess would be if something went wrong.
}

This way, you don't need any Windows Forms references in your project. It's even better if you want to define your app's window as the parent window for the folder selector. The Ookii one lets you use your Window instance as-is, whereas WinForms only accepts an IWin32Window.

0
Russ On

.NET 8.0 has now introduced the OpenFolderDialog.

https://devblogs.microsoft.com/dotnet/wpf-file-dialog-improvements-in-dotnet-8/

var folderDialog = new OpenFolderDialog
{
    Title = "Select Folder",
    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
};

if (folderDialog.ShowDialog() == true)
{
    var folderName = folderDialog.FolderName;
    MessageBox.Show($"You picked ${folderName}!");
}