ConfigAppSettings.cs
12.8 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.Xml;
using System.Windows.Forms;
using log4net;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace TSA_V.Common
{
public class ConfigAppSettings
{
public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static int seq = 1;
public static int nextSeq()
{
if (seq.Equals(Int32.MaxValue))
{
LOGGER.Info("seq当前值:" + seq + ",重置seq=0");
seq = 0;
}
Interlocked.Increment(ref seq);
return seq;
}
//public static string GetValue(string keyStr, string storeStr)
//{
// string key = keyStr + storeStr;
// System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// if (config.AppSettings.Settings[key] == null)
// {
// return GetValue(keyStr);
// }
// else
// {
// return config.AppSettings.Settings[key].Value;
// }
//}
public static decimal GetNumValue(string keyStr, string storeStr)
{
string key = keyStr + storeStr;
decimal a = 0;
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
return GetNumValue(keyStr);
}
else
{
{
Decimal.TryParse(config.AppSettings.Settings[key].Value, out a);
}
}
return a;
}
public static int GetIntValue(string keyStr, string storeStr,int defValue=0)
{
string key = keyStr + storeStr;
int a = defValue;
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
return GetIntValue(keyStr);
}
else
{
{
Int32.TryParse(config.AppSettings.Settings[key].Value, out a);
}
} return a;
}
public static uint getUintValue(string keyStr)
{
return (uint)GetIntValue(keyStr);
}
public static bool GetBoolValue(string keyStr,bool defv=false )
{
return GetIntValue(keyStr).Equals(1);
}
public static uint getUintValue(string keyStr, string storeStr)
{
return (uint)GetIntValue(keyStr, storeStr);
}
public static string GetValue(string key,string defValue="")
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LOGGER.Error("未找到配置:" + key + ",请检查配置是否完整!");
return defValue;
}
else
{
return config.AppSettings.Settings[key].Value;
}
}
public static decimal GetNumValue(string key)
{
decimal a = 0;
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LOGGER.Error("未找到配置:" + key + ",请检查配置是否完整!");
return a;
}
else
{
{
Decimal.TryParse(config.AppSettings.Settings[key].Value, out a);
}
}
return a;
}
public static double GetDoubleValue(string key)
{
return (double)GetNumValue(key);
}
public static int GetIntValue(string key,int defValue = 0)
{
int a = defValue;
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
LOGGER.Error("未找到配置:" + key + ",请检查配置是否完整!");
return a;
}
else
{
{
Int32.TryParse(config.AppSettings.Settings[key].Value, out a);
}
} return a;
}
//public static void SaveValue(string key, bool value)
//{
// if (value)
// {
// SaveValue(key, 1.ToString());
// }
// else
// {
// SaveValue(key, 0.ToString());
// }
//}
//public static void SaveValue(string key, double value)
//{
// SaveValue(key, value.ToString());
//}
//public static void SaveValue(string key, int value)
//{
// SaveValue(key, value.ToString());
//}
//public static void SaveValue(string key, string value)
//{
// try
// {
// if (key.Equals("") || value.Equals(""))
// {
// return;
// }
// //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
// System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// if (config.AppSettings.Settings[key] == null)
// {
// SetValue(key, value);
// }
// else
// {
// UpdateConfig(key, value);
// }
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, "SaveValue保存配置出错:AppKey=" + key + ",AppValue=" + value + "," + ex.StackTrace);
// }
//}
///// <summary>
///// 更新配置文件信息
///// </summary>
///// <param name="name">配置文件字段名称</param>
///// <param name="Xvalue">值</param>
//private static void UpdateConfig(string name, string Xvalue)
//{
// try
// {
// XmlDocument doc = new XmlDocument();
// doc.Load(Application.ExecutablePath + ".config");
// XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
// XmlElement ele = (XmlElement)node;
// ele.SetAttribute("value", Xvalue);
// doc.Save(Application.ExecutablePath + ".config");
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, "UpdateConfig保存配置出错:name=" + name + ",Xvalue=" + Xvalue + "," + ex.StackTrace);
// }
//}
/////<summary>
/////向.config文件的appKey结写入信息AppValue 保存设置
/////</summary>
/////<param name="AppKey">节点名</param>
/////<param name="AppValue">值</param>
//private static void SetValue(String AppKey, String AppValue)
//{
// try
// {
// XmlDocument xDoc = new XmlDocument();
// xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
// XmlNode xNode;
// XmlElement xElem1;
// XmlElement xElem2;
// xNode = xDoc.SelectSingleNode("//appSettings");
// xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
// if (xElem1 != null)
// xElem1.SetAttribute("value", AppValue);
// else
// {
// xElem2 = xDoc.CreateElement("add");
// xElem2.SetAttribute("key", AppKey);
// xElem2.SetAttribute("value", AppValue);
// xNode.AppendChild(xElem2);
// }
// xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, "SetValue保存配置出错:AppKey=" + AppKey + ",AppValue=" + AppValue + "," + ex.StackTrace);
// }
//}
//public static void UpdateAppSetting(string key, string value)
//{
// SaveValue(key, value);
//}
public static void BackFile()
{
//备份文件
//备份保存
try
{
string date = DateTime.Now.ToString("yyyy-MM-dd");
string targetBackPath = @"C:\configBack\" + date + @"\";
if (!Directory.Exists(targetBackPath))
{
Directory.CreateDirectory(targetBackPath);
}
string fileName = Process.GetCurrentProcess().ProcessName + ".exe.config";
string backFile = targetBackPath + fileName;
if (File.Exists(backFile))
{
File.Delete(backFile);
}
File.Copy(fileName, backFile, true);
}
catch (Exception e)
{
LOGGER.Error($"删除 appconfig 前备份出错:", e);
}
}
public static void RemoveKey(string keyName)
{
try
{
//删除配置文件键为keyName的项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(keyName);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
LogUtil.error($"删除 appconfig [{keyName}] 完成" );
}
catch (Exception ex)
{
LogUtil.error($"删除 appconfig [{keyName}] 出错:" + ex.ToString());
}
}
///// <summary>
///// 更新配置文件信息
///// </summary>
///// <param name="name">配置文件字段名称</param>
///// <param name="Xvalue">值</param>
//private static void UpdateConfig(string name, string Xvalue)
//{
// try
// {
// XmlDocument doc = new XmlDocument();
// doc.Load(Application.ExecutablePath + ".config");
// XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
// XmlElement ele = (XmlElement)node;
// ele.SetAttribute("value", Xvalue);
// doc.Save(Application.ExecutablePath + ".config");
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, "UpdateConfig保存配置出错:name=" + name + ",Xvalue=" + Xvalue + "," + ex.StackTrace);
// }
//}
/////<summary>
/////向.config文件的appKey结写入信息AppValue 保存设置
/////</summary>
/////<param name="AppKey">节点名</param>
/////<param name="AppValue">值</param>
//private static void SetValue(String AppKey, String AppValue)
//{
// try
// {
// XmlDocument xDoc = new XmlDocument();
// xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
// XmlNode xNode;
// XmlElement xElem1;
// XmlElement xElem2;
// xNode = xDoc.SelectSingleNode("//appSettings");
// xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
// if (xElem1 != null)
// xElem1.SetAttribute("value", AppValue);
// else
// {
// xElem2 = xDoc.CreateElement("add");
// xElem2.SetAttribute("key", AppKey);
// xElem2.SetAttribute("value", AppValue);
// xNode.AppendChild(xElem2);
// }
// xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
// }
// catch (Exception ex)
// {
// LogUtil.error(LOGGER, "SetValue保存配置出错:AppKey=" + AppKey + ",AppValue=" + AppValue + "," + ex.StackTrace);
// }
//}
}
}