Search code examples
wpffolderbrowserdialog

Using Folder Browser in WPF .NET 6.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.


Solution

  • 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.