0

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.

John
  • 3
  • 5
  • 1
    In .NET 6, if you want to use something from WinForms, you don't add an assembly reference, you set `` to `true` in your csproj, as explained [in the documentation](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props-desktop#enable-net-desktop-sdk). – Etienne de Martel May 07 '23 at 20:36
  • That worked great. Thanks! If you want to post it as an answer I will check it off. – John May 08 '23 at 17:13

2 Answers2

1

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.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
0

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);
  }
John
  • 3
  • 5