简体   繁体   中英

how to save settings in c# .net core 3.1 console application?

As usual, I added a Settings file to my application:
在此处输入图像描述

For now the file contains one datetime property which I assign after the application code ran through:
在此处输入图像描述

As usual, I assign the property as follows:

static void Main(string[] args)
{
    Properties.Default.LastUpdate = DateTime.Now;
}

Issue
Normally, I would then save the settings to file as follows but for some reason, VS does not like it:
在此处输入图像描述 在此处输入图像描述

full code as requested:

static void Main(string[] args)
{
    Properties.Default.LastUpdate = DateTime.Now;
    Properties.Default.Save();
    Properties.Default.Reload();
}

I would suggest to use the new configuration options provided by .net core.

-> Add appsettings.json to your project

appsettings.json

{
"name" : "Gary"
}

Init

IConfiguration config = new ConfigurationBuilder()
      .AddJsonFile("appsettings.json", true, true)
      .Build();

In this discussion you will also find a way how to update the value at runtime:

ASP.NET Core appsettings.json update in code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM