39

I'm migrating my WPF desktop app from .NET Framework to Core 3.0. I was using System.Windows.Forms.FolderBrowserDialog() and I'm now stuck on how to add this reference to the Core project. There is no "System.Windows.Forms" NuGet package available, is there? Is there any alternative way to display the FolderBrowserDialog in Core?

Update

I created the Core project using the default template and then copied and pasted .cs and .xaml files into it. The .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
Pang
  • 9,564
  • 146
  • 81
  • 122
yaugenka
  • 2,602
  • 2
  • 22
  • 41
  • Documentation says that class is present: https://learn.microsoft.com/pl-pl/dotnet/api/system.windows.forms.folderbrowserdialog?view=netcore-3.0 - please paste csproj file - maybe TargetFramework is wrong. – Lukasz Szczygielek Nov 13 '19 at 20:10
  • Please check the updated question. I saw the page as well, but the compiler gives the error `The type or namespace name 'Forms' does not exist in the namespace 'System.Windows'` and no reference import suggestions. – yaugenka Nov 13 '19 at 21:01

3 Answers3

85

You need to add to csproj an additional switch:

<UseWindowsForms>true</UseWindowsForms>

Add it below UseWpf. Then try rebuild. After this, you should be able to use Forms namespace.

Pang
  • 9,564
  • 146
  • 81
  • 122
Lukasz Szczygielek
  • 2,768
  • 2
  • 20
  • 34
11

And this is how to reference System.Windows.Forms in .NET 5.0 for WPF apps and WindowsForms

<PropertyGroup>
   <TargetFramework>net5.0-windows</TargetFramework>
   <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
   <UseWPF>true</UseWPF>
   <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
Mike
  • 14,010
  • 29
  • 101
  • 161
Amir Touitou
  • 3,141
  • 1
  • 35
  • 31
5

Looks like it already exists: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog?view=netcore-3.0

For other porting issues, you might want to use the Windows Compatibility Pack which is used to help port apps to .NET Core

There also might be some more information out there related to WPF and this issue, since it's been around for awhile. This might be helpful Select folder dialog WPF, and updated for .NET Core.

Good luck with your upgrade!

n234
  • 249
  • 1
  • 6