-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigurationManager.cs
More file actions
48 lines (42 loc) · 1.34 KB
/
ConfigurationManager.cs
File metadata and controls
48 lines (42 loc) · 1.34 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
using System;
using System.IO;
using System.Text.Json;
namespace Compiler
{
public class ConfigurationManager
{
private const string ConfigFileName = "config.json";
public Config LoadConfig()
{
try
{
if (!File.Exists(ConfigFileName))
{
var defaultConfig = new Config { };
string defaultJson = JsonSerializer.Serialize(defaultConfig, new JsonSerializerOptions()
{
WriteIndented = true
});
File.WriteAllText(ConfigFileName, defaultJson);
}
string json = File.ReadAllText(ConfigFileName);
return JsonSerializer.Deserialize<Config>(json)!;
}
catch (Exception ex)
{
Console.WriteLine($"Error loading config: {ex.Message}");
return new Config();
}
}
public static void SaveConfig(Config config)
{
// Serialize the config object to JSON
string json = JsonSerializer.Serialize(config, new JsonSerializerOptions
{
WriteIndented = true
});
// Write to the file
File.WriteAllText(ConfigFileName, json);
}
}
}