Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

c# - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using .NET 5 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...