How to reference System.Windows.Forms in .NET Core 3.0 for WPF apps?
Asked Answered
G

3

41

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>
Givens answered 13/11, 2019 at 20:5 Comment(2)
Documentation says that class is present: learn.microsoft.com/pl-pl/dotnet/api/… - please paste csproj file - maybe TargetFramework is wrong.Inca
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.Givens
I
87

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.

Inca answered 13/11, 2019 at 21:16 Comment(3)
It works! Thank you. Does the presense of both switches influence performance anyhow? It's only the FolderBrowserDialog that I need from the Forms.Givens
@Hostel how would one figure this out? And thanks, precisely what I needed.Treachery
You sir, just won the internet today! Yay! Cake!Rowden
S
12

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>
Stylobate answered 27/11, 2020 at 8:26 Comment(0)
P
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!

Potomac answered 13/11, 2019 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.