-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (43 loc) · 1.85 KB
/
Copy pathProgram.cs
File metadata and controls
53 lines (43 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Configuration.Writable;
using Configuration.Writable.FormatProvider;
using Example.ConsoleApp.NativeAot;
// initialize the writable config system
WritableOptions.Initialize(conf =>
{
// shared configuration for all options types
// JsonAotFormatProvider is the recommended format provider for NativeAOT scenarios
conf.FormatProvider = new JsonAotFormatProvider(SampleSettingSerializerContext.Default);
// if you want to standard system configuration location, use conf.UseStandardSaveDirectory("your-app-id");
// e.g. %APPDATA%\your-app-id\appdata-setting.json on Windows
// conf.UseStandardSaveDirectory("your-app-id");
// add SampleSetting
conf.Add<SampleSetting>(c =>
{
c.UseFile("./config/mysettings");
// if you want to customize the section name in the config file
// c.SectionName = "App:SampleSetting";
// use a custom validator to validate the config instance before saving
// (built-in DataAnnotations validation is disabled when building for NativeAOT)
c.WithValidator<SampleSettingValidator>();
});
});
// -------------------------------
// get the config instance
var options = WritableOptions.GetOptions<SampleSetting>();
var sampleSetting = options.CurrentValue;
Console.WriteLine($">> Name: {sampleSetting.Name}, LastUpdatedAt: {sampleSetting.LastUpdatedAt}");
// save the config instance
Console.Write(":: Enter new name: ");
var newName = Console.ReadLine();
await options.SaveAsync(setting =>
{
setting.Name = newName;
setting.LastUpdatedAt = DateTime.Now;
});
Console.WriteLine(":: Config saved.");
Console.WriteLine($" at {options.ConfigurationInfo.WritePath}");
// get updated config instance
var updatedSampleSetting = options.CurrentValue;
Console.WriteLine(
$">> Name: {updatedSampleSetting.Name}, LastUpdatedAt: {updatedSampleSetting.LastUpdatedAt}"
);