-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrefs.cs
More file actions
51 lines (40 loc) · 1.27 KB
/
Prefs.cs
File metadata and controls
51 lines (40 loc) · 1.27 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
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace ChatAppClient;
public static class Prefs {
private static Dictionary<string, string> prefs;
public static string GetString(string key) {
if (prefs == null) {
// Load prefs from disk
Load();
}
return !prefs.ContainsKey(key) ? null : prefs[key];
}
public static string GetString(string key, string defaultValue) => GetString(key) ?? defaultValue;
public static void SetString(string key, string value) {
if (prefs == null) {
// Load prefs from disk
Load();
}
prefs[key] = value;
}
// Load prefs function
private static void Load() {
if (!File.Exists("prefs.json")) {
prefs = new Dictionary<string, string>();
return;
}
// Load prefs from disk
prefs = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText("prefs.json"));
}
// Save prefs function
public static void Save() {
if (prefs == null) {
// Nothing to save
return;
}
// Save prefs to disk
File.WriteAllText("prefs.json", JsonSerializer.Serialize(prefs));
}
}