How to Migrate a .NET Fx WinForms App to .NET Core 3.1

If you have any application built in .NET framework and you want to change your application to a .NET core application. So, let’s see how we can start the migration from the .NET framework to .NET core.
 
I have an existing .NET Win form app with an additional NuGet package for NUnit, it might be possible your application can have many NuGet Packages along with the default .NET assemblies so we can use all of them into a .NET core app if they are supporting .NET core.
 
Now, let me show you my .NET Win Form Application.
 
This is my solution for the "Windows Forms application" where you can see all the default .NET assemblies and NuGet Packages.
 
How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
When I run this application, you will get the following output. In this application, I have just one win-form with a button to open a dialog for selecting an image to show in a picture box.
 
How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
This application is working fine until now. Now, we must move this along with all the dependencies into .NET core.

For .NET core migration, you can follow the following steps,
 
Step 1
 
Before starting the migration, we must check the compatibility of this application with .NET core. For checking this, we need an application named Portability Analyzer, which is freely available on GitHub, or, you can use the below link.
 
 
Once you get the app, run it. You should get this screen,
 
How to Migrate a .Net Fx WinForms App to .Net Core 3.1

Now select your application folder with this browse button on the top right side of the screen. After, click on the button “Analyze”

How to Migrate a .Net Fx WinForms App to .Net Core 3.1

This will give you an Excel file with the report on migration. Open that Excel file. This file will have two sheets, in the first sheet you’ll get the report in percentage (%), that will show the compatibility with .NET core of all the assemblies is being used by this project.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1

If all are green with 100% so you can directly jump to the next step. If not, go and check https://Nuget.org with the package name. In this application, I have NUnit, so I searched for the same.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
If you see that all your dependencies are supporting .NET core. Now jump to the next step. If not, try to find some alternative option for this package that supports .NET core. Add that and make changes to the classes if required.
 
Step 2
 
This step is optional only for those who have any other NuGet packages. Find the packages.config file in your project solution and right-click on this file.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Find the option “Migrate packages.config to PackageRefrence… “.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
There should be a confirmation window, just press enter on the OK button.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
It will open the NuGet Migration report as an HTML document in your default browser. You don’t have to anything with this file, just close is after review.
 
After this, your packages.config file will be no longer in your project all the information will move into the “.csproj” file.
 
Step 3
 
Now right-click on your project name and find the unload this project so that we can edit the “.csproj” file.
 
How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Right-click on the project name after unloading it and click the Edit option.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Now that you have all the project settings, select all and cut from this file and paste in notepad for some time, because we need our NuGet packages information from it in a moment.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Now, your project file must be empty as we must use this file for the required settings of .NET core.
 
We need to set four properties for .NET core projects like Output Type, Target Framework, Use Windows form and Generate Assembly Info.

You can paste the following code or type them manually.
  1. <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">    
  2.   <PropertyGroup>    
  3.     <OutputType>WinExe</OutputType>    
  4.     <TargetFramework>netcoreapp3.1</TargetFramework>    
  5.     <UseWindowsForms>true</UseWindowsForms>    
  6.     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>    
  7.   </PropertyGroup>    
  8. </Project>    
Your project file should look now like this.
 
How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Now open the old code which you cut from this project file and find an Item group where your dependency is added. In my case, I have just one NuGet dependency of NUnit, so I have added that Item group from the old code of this project.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
After this save all the changes.
 
Step 4

Now, for the last step, just reload this project.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Open solution explorer and the new solution, you’ll see the changes as your project have all the Dependencies required for .NET core application along with NuGet packages.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
Just build your app and run this.

How to Migrate a .Net Fx WinForms App to .Net Core 3.1
 
In my case, this application is still working fine with .NET core. I hope this will work for you too.
 
Thanks.