简体   繁体   English

如何在 c# .net core 3.1 控制台应用程序中保存设置?

[英]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.

相关问题 c# .Net Core 3.1 Azure App Service Windows - 应用程序无法检索应用程序设置 - c# .Net Core 3.1 Azure App Service Windows - Application not able to retrieve App settings 如何在 Linux 上创建没有控制台的 C#.Net Core 应用程序? - How to create a C# .Net Core application without a console on Linux? 如何在带有 .Net3.1 的 C# 控制台应用程序中使用 docker 创建 serilog 记录器文件 - How to create a serilog logger file using docker in C# console Application with .Net3.1 如何在c#应用程序设置中保存byte [] - How to save byte[] in c# application settings 在 C# ASP.NET Core 3.1 应用程序中,我如何路由到 controller 然后查询字符串? - In a C# ASP.NET Core 3.1 application how do I route to a controller then query string? 如何在 C# .NET Core 3 中使用 web sockets。 - How to use web sockets in C# .NET Core 3.1? 如何在 c# .NET core 3.1 中延迟某个任务 - How to delay a certain task in c# .NET core 3.1 如何在 C# .net Core 3.1 中实例化一个 Logger - How to instance an Logger in C# .net Core 3.1 C# Console Application.Net core中的分层数据结构菜单 - Hierarchical Data Structure Menu in C# Console Application .Net core 如何为 ASP.NET Core 3.1 配置 Azure App Service 应用程序设置? - How to configure Azure App Service application settings for ASP.NET Core 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM