.NET Core中的用户配置设置

ano*_*non 8 c# settings configuration .net-core

我昨天花了一整天时间研究这个并没有找到任何合理的解决方案.

我正在将.NET Framework项目移植到.NET Core 2.0.该项目使用用户设置(Properties.Settings.Default)来存储通过每次应用程序启动而持久存储的某些信息.它还可以更新此信息(例如用户首选项选项).

我的理解是.NET Core中不存在?那怎么可能实现这个呢?我能找到的最接近的东西是安装一些NuGet包并使用Microsoft.Extensions.Configuration,但是,这Properties.Settings.Default在.NET Framework中甚至都不是很接近.

首先,它需要一个文件(JSON,XML,你有什么)在运行文件的目录中.这看起来很疯狂,特别是在安全信息的情况下.另外,我不想要把配置文件与项目时,我的项目的内部图书馆应该能够应付这一切,因为它与.NET框架一样.

我意识到有环境变量,但我无法让它们为我的生活加载.老实说,这应该是一个简单的过程,所以我不确定我是否遗漏了一些明显的东西.在这里,在我的库的属性>调试部分,有一个名为"环境变量"的部分,我在其中添加了两个值.

环境变量示例

我的配置对象如下所示:

private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
    //.SetBasePath(Directory.GetCurrentDirectory())
    //.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();
Run Code Online (Sandbox Code Playgroud)

要访问变量,我尝试过这些方法:

Configuration.GetSection("SettingName").Value;
Configuration["SettingName"];
Environment.GetEnvironmentVariable("SettingName");
Run Code Online (Sandbox Code Playgroud)

他们都回来了.

还有,没有办法保存财产吗?我知道它的设计重量轻,但肯定有一种方法可以节省吗?

我试过了:

Configuration["SettingName"] = "Value";
Environment.SetEnvironmentVariable("SettingName", "Value");
Run Code Online (Sandbox Code Playgroud)

它似乎更新了内存,但不是文件本身,这使得持久设置无用.

如何在库中读取和保存用户设置,同时将其全部包含在库中?

小智 3

我创建了一个静态类,用户可以根据需要在任何地方访问该类。这并不完美,但它是强类型的,并且仅在访问默认属性时创建。

我打算使用 DI 将配置放入每个类的属性中,但在某些情况下我希望将类包含在构造函数中。

使用:

public MainWindow()
{
    InitializeComponent();

    var t = AppSettings.Default.Theme;
    AppSettings.Default.Theme += "b";
    AppSettings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)

班上:

using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TestClient
{

    public class AppSettings
    {
        private AppSettings()
        {
            // marked as private to prevent outside classes from creating new.
        }

        private static string _jsonSource;
        private static AppSettings _appSettings = null;
        public static AppSettings Default
        {
            get
            {
                if (_appSettings == null)
                {
                    var builder = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

                    _jsonSource = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}appsettings.json";

                    var config = builder.Build();
                    _appSettings = new AppSettings();
                    config.Bind(_appSettings);
                }

                return _appSettings;
            }
        }

        public void Save()
        {
            // open config file
            string json = JsonConvert.SerializeObject(_appSettings);

            //write string to file
            System.IO.File.WriteAllText(_jsonSource, json);
        }

        public string Theme { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)