14

I have a Windows Forms application targeted for .NET Framework 4.7.2. As .NET Core 3.0 supports Windows Forms I'd like to try it. Is there any way to convert VS2019 project from .NET Framework to .NET Core or the only way possible is to create a new .NET Core project and add files from the old one?

I'm asking only about VS project migration, not any potential incompatibility issues.

jacekbe
  • 499
  • 1
  • 5
  • 18
  • 4
    As shown in todays .NET Conf videos (see the live stream currently running on https://www.dotnetconf.net/, rewind back to around 1:27-1:28 (1 hour + 27-28 minutes) into the video and watch), there is a new tool called "try-convert" that can attempt to make the conversion for you. How to install this tool, where to get it, I have no idea, it may be evident if you have Visual Studio 2019 and .NET Core 3 SDK installed though, which is why I leave this as a comment and not an answer. – Lasse V. Karlsen Sep 23 '19 at 18:18
  • https://learn.microsoft.com/en-us/dotnet/core/porting/winforms. Caveat, the winform designers don't support .NET Core yet, so you might also want to look into the latest VS 2019 Preview drop as well. – Ed Dore Sep 23 '19 at 19:57
  • Olia Gavrysh (lady doing the presentation) said they will publish it tomorrow - 7 hours ago :) I will paste link as soon it is available. – watbywbarif Sep 24 '19 at 15:01
  • 1
    It could be interesting to test Winform migration .Net Core 3.0 for a future upgrade, but I wouldn't recommand it on a huge application that already have to run smoothly on production environment. Even for testing purpose, if you're not in haste, at least wait for a few weeks the official availability of required tools, it will be easier. – AFract Sep 24 '19 at 15:08
  • @LasseVågsætherKarlsen I have Visual Studio 2019 with .NET Core 3 and there is no such file on disk. I think they even said as tool is not 100% successful it will not be part of official VS install. – watbywbarif Sep 25 '19 at 09:11
  • Any updates on the link to the migration tool? – Andrey Sep 30 '19 at 03:43
  • @Andrey check my answer below, relased... – watbywbarif Oct 03 '19 at 12:42

4 Answers4

11

[EDIT] Official tool released: try-convert

Check out releases for latest version.

Until official tool is published, here is link from some guy that has made alternative Migration-von-NET-Framework-zu-NET-Core-per-PowerShell

watbywbarif
  • 6,487
  • 8
  • 50
  • 64
  • 6
    The try-convert is not an official tool. They write: "This tool is not supported in any way" and "This is an open source project built by members of the .NET team in their spare time" – Ted Feb 02 '20 at 20:44
  • What is the command for upgrading framework from net20 to net47? – Karthic G Mar 10 '21 at 16:48
  • getting error as "app.csproj contains a reference to System.Web, which is not supported on .NET Core. You may have significant work ahead of you to fully port this project" while applying command "try-convert -w app.csproj" what we do, is this possible force to migrate? – Karthic G Mar 10 '21 at 16:49
1

Please follow this step:

Using Portability Analyzer

Use the following instructions to run Portability Analyzer.

1.Run PortabilityAnalyzer.exe (https://github.com/Microsoft/dotnet-apiport-ui/releases/download/1.1/PortabilityAnalyzer.zip)

2.In the Path to application text box enter the directory path to your Windows Forms or WPF app (either by inserting a path string or clicking on Browse button and navigating to the folder).

3.Click Analyze button.

4.After the analysis is complete, a report of how portable your app is right now to .NET Core 3.0 will be saved to your disc. You can open it in Excel by clicking Open Report button.

NET Portability Analyzer to determine if there are any APIs your application depends on that are missing from .NET Core. If there are, you need to refactor your code to avoid dependencies on APIs, not supported in .NET Core. Sometimes it’s possible to find an alternative API that provides the needed functionality.

Migration

1.Replace packages.config with PackageReference. If your project uses NuGet packages, you need to add the same NuGet packages to the new .NET Core project. .NET Core projects support only PackageReference for adding NuGet packages. To move your NuGet references from packages.config to your project file, in the solution explorer right-click on packages.config -> Migrate packages.config to PackageReference…..

2.Migrate to the SDK-style .csproj file. To move my application to .NET Core, first I need to change my project file to SDK-style format because the old format does not support .NET Core. Besides, the SDK-style format is much leaner and easier to work with.Make sure you have a copy of your current .csproj file. Replace the content of your .csproj file with the following.

For WinForms application:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

For WPF application

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <UseWPF>true</UseWPF>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

3.Move from .NET Framework to .NET Standard or .NET Core. To do so, replace this

<TargetFramework>net472</TargetFramework>

with

<TargetFramework>netstandard2.0</TargetFramework>

or

<TargetFramework>netcoreapp3.0</TargetFramework>

4.In the project file copy all external references from the old project, find the proejct reference using old csproj file relpace this. for example:

<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

to .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

<ItemGroup>
     <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>

1

Microsoft released a new Upgrade Assistant tool that will migrate your .NET Framework projects to .NET 5. After migration you can change the TargetFramework in your csproj files to whatever version of .NET you want.

I ran the following commands to install the tool:

dotnet tool install -g try-convert
dotnet tool install -g upgrade-assistant

Then to use it:

upgrade-assistant .\MyProject.csproj
Shahin Dohan
  • 6,149
  • 3
  • 41
  • 58
0

Maybe in some cases brutal force migration (mentioned in OP) is indeed easiest especially when there is 3rd party libaries that can't be processed by tools automatically.

My Winforms (.NET 4.6.2 with VS2019) exampe:

(1) Create new VS2019 .net core 3.0 WinForm project.

(2) Copy and overwrite every file except for the .csproj and .csproj.user from the old project to the new project folder.

(3) VS2019 somehow add all the copied files to the new project automatically (in this case some file not included in the old project need to be removed manually). If not, add them to the project.

(4) Per the errors in the new project, add missing references.

(5) Some code need to be modified manually such as MySql.Data.MySqlClient -> MySqlConnector.

(6) Upgrade to VS2019 16.8 for support for Form editor with .NET core.

jw_
  • 1,663
  • 18
  • 32