-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConfigFileReadercs.cs
More file actions
74 lines (53 loc) · 1.77 KB
/
ConfigFileReadercs.cs
File metadata and controls
74 lines (53 loc) · 1.77 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
namespace SmartChain.Web
{
public class ConfigFileReader
{
private JObject options = new JObject();
public ConfigFileReader()
{
string path = Directory.GetCurrentDirectory().ToString() + "/config.conf";
if (!File.Exists(path)){
path = Directory.GetCurrentDirectory().ToString() + "/default.conf";
}
if (!File.Exists(path))
return;
using (StreamReader sr = new StreamReader(path))
{
string json = sr.ReadToEnd();
try {
options = JObject.Parse(json);
}
catch(Exception e)
{
}
}
}
public string lookup(string key)
{
if (!options.ContainsKey(key))
throw new Exception("The key '" + key + "' was not set in the config: " + Directory.GetCurrentDirectory().ToString());
return options[key].ToString();
}
public JArray lookupArray(string key)
{
if (!options.ContainsKey(key))
throw new Exception("The key '" + key + "' was not set in the config: " + Directory.GetCurrentDirectory().ToString());
return (JArray) options[key];
}
public string lookup_or_default(string key)
{
if (!options.ContainsKey(key))
return "";
return options[key].ToString();
}
public bool haskey(string key)
{
return options.ContainsKey(key);
}
}
}