-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathConfig.cs
90 lines (75 loc) · 1.83 KB
/
Config.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Newtonsoft.Json;
namespace SystemCommandsBot;
public class Config
{
public Config()
{
Commands = new List<Command>();
}
public string Password { get; set; }
public string ApiKey { get; set; }
public List<Command> Commands { get; set; }
public void LoadDefaultValues()
{
ApiKey = "";
Commands.Add(new Command
{
Id = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start",
MaxInstances = 2
});
}
public static Config Load()
{
try
{
return Load(AppContext.BaseDirectory + "config\\default.cfg");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public static Config Load(string path)
{
try
{
var cfg = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
return cfg;
}
catch (DirectoryNotFoundException)
{
var di = new DirectoryInfo(path);
if (!Directory.Exists(di.Parent.FullName))
{
Directory.CreateDirectory(di.Parent.FullName);
}
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (FileNotFoundException)
{
var cfg = new Config();
cfg.LoadDefaultValues();
cfg.Save(path);
return cfg;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public void Save(string path)
{
try
{
File.WriteAllText(path, JsonConvert.SerializeObject(this));
}
catch
{
}
}
}