Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding configuration to windows forms on .NET 5.0

I'm migrating an existing windows forms C# app to .NET 5.0 and I'm trying to follow the instrutions presented on the migration docs. Everything is working ok, but there's still one thing to do: migrate the debug/release settings from app.config files.

I've thought about reusing NET Core's IConfiguration, but adding the Microsoft.Extensions.Configuration nuget package to the project (so that I'm able to create a ConfigurationBuilder instance) seems to break everything (for instance, using System; will start generating compile errors).

Any ideas on what's going on? How are you guys migrating the settings from 4.8 to .NET 5.0 on Windows Forms apps?

like image 607
Luis Abreu Avatar asked Jan 11 '21 15:01

Luis Abreu


1 Answers

Using .NET 5, .NET 6 or .NET Core configuration system in Windows Forms

You can follow these steps:

  1. Create a WinForms .NET (5) Application

  2. Install Microsoft.Extensions.Hosting package.

    Instead of the hosting package you may want to install Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder which are sufficient for this example.

  3. Add an appsettings.json file to the project root, set its build action to Content and Copy to output directory to Always.

  4. Modify the program class:

    static class Program
    {
        public static IConfiguration Configuration;
        static void Main(string[] args)
        {
            //To register all default providers:
            //var host = Host.CreateDefaultBuilder(args).Build();
             var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            Configuration = builder.Build();
             Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    

    Make sure you have added using Microsoft.Extensions.Configuration;

  5. Set the content of the file:

    {
      "MySettings": {
        "Text": "Title of the form",
        "BackColor": "255,0,0",
        "Size": "300,200"
      }
    }
    
  6. To read the setting, open Form1.cs and paste the following code:

    public class MySettings
    {
        public string Text { get; set; }
        public Color BackColor { get; set; }
        public Size Size { get; set; }
     }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var mySettings = Program.Configuration.GetSection("MySettings").Get<MySettings>();
        this.Text = mySettings.Text;
        this.BackColor = mySettings.BackColor;
        this.Size = mySettings.Size;
    }
    
  7. Run the application and see the result:

    enter image description here

Using Classic Settings for Windows Forms

And to answer your last question: How are you guys migrating the settings from 4.8 to .NET 5.0 on Windows Forms apps?

It looks like you are familiar with application/user settings in .NET 4.x. The same is still supported in .NET 5. Settings.settings file is part of the default project template and it allows you to create user settings and application settings with designer support and many more features. You can look at Application Settings for Windows Forms.

like image 177
Reza Aghaei Avatar answered Nov 07 '22 11:11

Reza Aghaei