[英]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:通常,我会将设置保存到文件中,如下所示,但出于某种原因,VS 不喜欢它:
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.我建议使用 .net 内核提供的新配置选项。
-> Add appsettings.json to your project -> 将 appsettings.json 添加到您的项目中
appsettings.json 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 ASP.NET 核心 appsettings.json 更新代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.