0

I am in process of replacing Settings.settings file with appsettings.json and this is what I have done

        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            using IHost host = CreateHostBuilder().Build();
            System.Windows.Forms.Application.Run(host.Services.GetRequiredService<XtraMain>());
        }
         static IHostBuilder CreateHostBuilder() =>
         Host.CreateDefaultBuilder()
        .ConfigureAppConfiguration((context, builder) =>
        {
            builder.AddJsonFile("apsettings.json", optional: true, reloadOnChange: true);
        })
        .ConfigureServices((context, services) =>
        {
            services.AddScoped<XtraMain>();
            services.AddPersistenceServices(context.Configuration);
            services.Configure<AppSettings>(context.Configuration.GetSection(nameof(AppSettings)));
        });

And I add this AppSettings class to store data from AppSettings section in apsettings

    public class AppSettings
    {
        public  string Palette { get; set; }
        public  string ThemeName { get; set; } 
    }

and this my AppSettings

    {
    "AppSettings": {
        "Palette": "Default",
        "ThemeName": "Office 2019 Colorful"
    }
    }

and in my Main form

private readonly AppSettings settings;
public XtraMain(IOptions<AppSettings> settings)
{
    InitializeComponent();
    this.settings = settings.Value;
}
public void GetDataFromAppsettings()
{
    UserLookAndFeel.Default.SkinName = settings.ThemeName;
    UserLookAndFeel.Default.SetSkinStyle(settings.ThemeName, settings.Palette);
}
public void SetAppsettings()
{
  //modify appsettings.json Programmatically
}

How can I modify appsettings.json Programmatically

M.Bouabdallah
  • 530
  • 10
  • 29
  • Hope the duplicate will help you. However I feel the need to say. Why repeat the same design mistake again?. AppSettings should be left alone for configuring your application from the outside. User settings should be on some other kind of persistent storage like your own user's settings file or some database table if they are common to all your users. Though it is just my opinion. – Steve Mar 22 '22 at 18:41
  • Thanks, Steve for the advice just to make it clear, did you mean that as best practices I should save these settings on database rather than save it on appsettings – M.Bouabdallah Mar 22 '22 at 19:02
  • 1
    I would not touch appsettings.json from the program itself and use it only for elements that doesn't change in the runtime lifetime. In Framework we have this distinction between applicationSettings and userSettings where the former was for application static configuration and the later for user specific configuration. In Core we have different providers that could do the same. Look for example to this https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?msclkid=a65c791aaa1411ec9da378d819709822&view=aspnetcore-6.0#file-configuration-provider – Steve Mar 22 '22 at 19:21
  • 1
    While for a brute force solution (rewrite all appsettings.json) you can take a look at this one https://stackoverflow.com/questions/51351464/user-configuration-settings-in-net-core?msclkid=a65c25a9aa1411ec8f516bd9fb68eb40 – Steve Mar 22 '22 at 19:22
  • Thank you for the helpful links, I was going to ask you about user settings like username and password so he can login automatically. How can I deal with this scenario? Sorry for so many questions. – M.Bouabdallah Mar 23 '22 at 05:15

0 Answers0