ConfigRW.cs
4.4 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
namespace DAL
{
public class ConfigRW
{
private readonly Dictionary<string, string> config = new();
private readonly System.Xml.XmlDocument doc = new();
private readonly System.Xml.XmlNode root;
private readonly string filePath;
private bool changed = false;
public ConfigRW(string path)
{
filePath = path;
Model.LogNet.log.Info($"读取配置文件");
Model.LogNet.log.Debug($"配置文件目录:{path}");
try
{
doc.Load(filePath);
root = doc.SelectSingleNode("appSettings");
for (int i = 0; i < root.ChildNodes.Count; i++)
{
string key = root.ChildNodes[i].Name;
string value = root.ChildNodes[i].InnerText;
config.Add(key, value);
}
}
catch (Exception ex)
{
Model.LogNet.log.Error("ConfigRW", ex);
}
}
public void Read<T>(string key, out T value)
{
value = (T)TypeDefault(typeof(T));
bool rtn = config.TryGetValue(key, out string str);
if (rtn)
{
if (TypeConv(typeof(T), str, out object obj))
value = (T)obj;
}
//Model.LogNet.log.Debug($"Read Config {key}:{value}");
}
public T Read<T>(string key)
{
Read(key, out T value);
return value;
}
public T Read<T>(string key, T defaultValue)
{
T value = defaultValue;
bool rtn = config.TryGetValue(key, out string str);
if (rtn)
{
if (TypeConv(typeof(T), str, out object obj))
value = (T)obj;
}
//Model.LogNet.log.Debug($"Read Config {key}:{value}");
return value;
}
public void Write<T>(string key, T value)
{
if (config.ContainsKey(key))
{
config[key] = value.ToString();
root.SelectSingleNode(key).InnerText = config[key];
}
else
{
config.Add(key, value.ToString());
System.Xml.XmlElement node = doc.CreateElement(key);
node.InnerText = config[key];
root.AppendChild(node);
}
changed = true;
Model.LogNet.log.Debug($"Write Config {key}:{value}");
}
public void Save()
{
if (changed)
{
doc.Save(filePath);
changed = false;
Model.LogNet.log.Info("Save Config");
}
}
private bool TypeConv(Type type, string str, out object obj)
{
bool rtn = false;
obj = null;
if (type == typeof(string))
{
obj = str;
rtn = true;
}
else if (type == typeof(int))
{
if (int.TryParse(str, out int result))
{
obj = result;
rtn = true;
}
}
else if (type == typeof(float))
{
if (float.TryParse(str, out float result))
{
obj = result;
rtn = true;
}
}
else if (type == typeof(double))
{
if (double.TryParse(str, out double result))
{
obj = result;
rtn = true;
}
}
else if (type == typeof(bool))
{
if (bool.TryParse(str, out bool result))
{
obj = result;
rtn = true;
}
}
return rtn;
}
private object TypeDefault(Type type)
{
if (type == typeof(string))
return "";
else if (type == typeof(int) || type == typeof(float) || type == typeof(double))
return 0;
else if (type == typeof(bool))
return false;
else
return null;
}
}
}