-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigHelper.cs
90 lines (77 loc) · 2.73 KB
/
ConfigHelper.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.Linq;
namespace HexoArticleEditor
{
/// <summary>
/// 配置读取帮助类
/// </summary>
public static class ConfigHelper
{
/// <summary>
/// 配置文件路径
/// </summary>
public static string ConfigFileName { get; set; } = @"Config.json";
private static object ReadLock { get; set; } = new object();
private static object WriteLock { get; set; } = new object();
/// <summary>
/// 读取配置
/// </summary>
/// <param name="sectionName">需要读取的配置键名</param>
/// <typeparam name="T">类型</typeparam>
/// <returns>目标类型的配置</returns>
public static T GetConfig<T>(string sectionName, T defaultValue = default)
{
lock (ReadLock)
{
if (File.Exists(ConfigFileName) is false)
{
File.WriteAllText(ConfigFileName, "{}");
}
var o = JObject.Parse(File.ReadAllText(ConfigFileName));
if (o.ContainsKey(sectionName))
{
return o[sectionName].ToObject<T>();
}
if (defaultValue != null)
{
SetConfig<T>(sectionName, defaultValue);
return defaultValue;
}
if (typeof(T) == typeof(string))
{
return (T)(object)"";
}
if (typeof(T) == typeof(int))
{
return (T)(object)0;
}
if (typeof(T) == typeof(long))
{
return default;
}
return typeof(T) == typeof(bool)
? (T)(object)false
: typeof(T) == typeof(List<>) ? defaultValue : typeof(T) == typeof(object) ? (T)(object)new { } : throw new Exception("无法默认返回");
}
}
public static void SetConfig<T>(string sectionName, T value)
{
lock (WriteLock)
{
if (File.Exists(ConfigFileName) is false)
{
File.WriteAllText(ConfigFileName, "{}");
}
var o = JObject.Parse(File.ReadAllText(ConfigFileName));
if (o.ContainsKey(sectionName))
{
o[sectionName] = JToken.FromObject(value);
}
else
{
o.Add(sectionName, JToken.FromObject(value));
}
File.WriteAllText(ConfigFileName, o.ToString(Newtonsoft.Json.Formatting.Indented));
}
}
}
}