How to convert Windows Forms applications from .NET Framework 4.8 to .NET 5

I have two Windows Forms applications in GitHub that I created many moons ago using the .NET Framework.

ProLexia – which is a transparent, coloured screen overlay that helps people with dyslexia
Syncify – which re-titles MP3s so that they play in the correct order in my car (I’m looking at you Ford)

I’ve kept them up to date with .NET (and Visual Studio) versions and when I last tweaked them, they were running on .NET Framework 4.8

Until recently I figured that that’s how they’d stay. They both use classic Windows Forms and both use the Windows API (via P/Invoke) – they’re not going to work on .NET Core right?

Wrong!

.NET Core 3.1 and above supports both Windows Forms and P/Invoke, though the Windows Forms editor is still a bit flaky.

So how much work was it to convert from .NET Framework 4.8 to .NET 5? Laughably little, it turns out.

Here are the changes you need to make:
  • Create a new project file (.csproj). This is the biggest change, partly because the new format contains so little. I created a new project using the Visual Studio new Windows Form project wizard.
  • The Windows Forms template added an extra line to my Program.cs file:
    Application.SetHighDpiMode(HighDpiMode.SystemAware);
    It’s up to you if you want to keep it
  • By default there’s no Properties folder, but traditional Resources and Settings files can be created from the project properties page – file versioning is also done from here
  • I then copied in the source files from my original version. Just copy and paste in Windows Explorer, Visual Studio will automatically add them to the project.
  • No need to add packages.config though, NuGet is handled differently…
  • Use NuGet to add missing references. While you’re there add in the CSharp and StyleCop code analyzers – because we all want clean, maintainable code!
  • Lastly, you may need to update form controls. These are the one I needed to update:
    • ContextMenu is now ContextMenuStrip (and has a MenuItems collection rather than an Items collection)
    • MenuItem is now MenuItemStrip (and no longer has an Index property)

And that is it!

You should now be able to compile, debug and run up your Windows Forms applications as before.

The last thing I chose to do was to set up a publish profile targeting a local folder. This is so that I could generate the files I needed to zip up and add to my download page.

Comments