-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalConfigManager.cs
42 lines (37 loc) · 1.06 KB
/
LocalConfigManager.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
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LocalConfig
{
public class LocalConfigManager
{
private StreamReader _reader;
private string _path;
public LocalConfigManager(string location)
{
_path = location;
if (!File.Exists(location)) File.Create(location);
}
public void WriteConfig(object @object)
{
var content = JsonConvert.SerializeObject(@object);
File.WriteAllText(_path, content);
}
public T ReadConfig<T>()
{
_reader = new StreamReader(_path, Encoding.UTF8);
var content = _reader.ReadToEnd();
_reader.Close();
return JsonConvert.DeserializeObject<T>(content);
}
public object ReadProperty(string name)
{
_reader = new StreamReader(_path, Encoding.UTF8);
var content = _reader.ReadToEnd();
_reader.Close();
return JObject.Parse(content)[name];
}
}
}