-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettingsService.cs
More file actions
62 lines (47 loc) · 1.44 KB
/
Copy pathAppSettingsService.cs
File metadata and controls
62 lines (47 loc) · 1.44 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
54
55
56
57
58
59
60
61
62
using System.Text.Json;
namespace GroupDynamic;
public sealed class AppSettings
{
public AppTheme Theme { get; set; } = AppTheme.System;
public string? LastDirectory { get; set; }
public int WindowLeft { get; set; }
public int WindowTop { get; set; }
public int WindowWidth { get; set; }
public int WindowHeight { get; set; }
public bool IsMaximized { get; set; }
}
public static class AppSettingsService
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
WriteIndented = true
};
public static AppSettings Load()
{
string settingsPath = GetSettingsPath();
if (!File.Exists(settingsPath))
{
return new AppSettings();
}
try
{
string json = File.ReadAllText(settingsPath);
return JsonSerializer.Deserialize<AppSettings>(json, SerializerOptions) ?? new AppSettings();
}
catch
{
return new AppSettings();
}
}
public static void Save(AppSettings settings)
{
string settingsPath = GetSettingsPath();
Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)!);
string json = JsonSerializer.Serialize(settings, SerializerOptions);
File.WriteAllText(settingsPath, json);
}
private static string GetSettingsPath()
{
return Path.Combine(Application.UserAppDataPath, "settings.json");
}
}