-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigLoader.cs
53 lines (49 loc) · 1.75 KB
/
ConfigLoader.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
using System;
using System.IO;
using Newtonsoft.Json;
namespace SpotifySongTracker {
public class ConfigLoader {
private string formatDefault = " {ARTIST} - {SONG} ";
private string prependedTextDefault = "";
private string appendedTextDefault = "";
private string spotifyNotOpenMessageDefault = "Spotify is not open...";
private string noSongPlayingMessageDefault = "No song currently playing...";
private string outputFileNameDefault = "output.txt";
private bool outputAlbumArtDefault = true;
private JsonSerializer serializer = new JsonSerializer();
public Config LoadConfigFromFile(string fileName) {
StreamReader file = null;
Config config;
try {
using (file = File.OpenText(string.Format(@"{0}", fileName))) {
config = (Config)serializer.Deserialize(file, typeof(Config));
}
} catch (Exception error) {
ErrorLogger.LogToFile(error);
config = new Config() {
format = formatDefault,
prependedText = prependedTextDefault,
appendedText = appendedTextDefault,
spotifyNotOpenMessage = spotifyNotOpenMessageDefault,
noSongPlayingMessage = noSongPlayingMessageDefault,
outputFileName = outputFileNameDefault,
outputAlbumArt = outputAlbumArtDefault
};
UpdateConfigFile(config, fileName);
} finally {
if (file != null) {
file.Close();
}
}
return config;
}
public void UpdateConfigFile(Config config, string fileName) {
string json = JsonConvert.SerializeObject(config, Formatting.Indented);
try {
File.WriteAllText(fileName, json);
} catch (Exception error) {
ErrorLogger.LogToFile(error);
}
}
}
}