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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Misc
{
/// <summary>
///
/// 用于备份!!!
/// <para>流程:</para>
///
/// <para>系统启动:</para>
///
/// <para>BackupFile.Add("profile.xml", func_isFileOK);</para>
/// <para>BackupFile.Add("initparam.xml", func_isFileOK);</para>
/// <para>BackupFile.Add("renzijiascan.xml", func_isFileOK);</para>
/// <para />
/// <para>CheckResult cr = BackupFile.Check()//检测,并处理</para>
/// <para>switch(cr)//根据结果显示在界面</para>
/// </summary>
public class BackupFile
{
public delegate bool IsFileOKHandler(string filepath);
class FileInfo
{
public int groupno;
public string Path;
public string FileName;
public IsFileOKHandler func_isFileOK;
public bool hasCheck = false;
public CheckResult cr = CheckResult.OK;
public void Backup()
{
hasCheck = true;
if (!Directory.Exists("backup"))
Directory.CreateDirectory("backup");
if (File.Exists(Path))
{
string sourceFileName = Path;
string destFileName = @"backup\" + FileName;
System.IO.FileInfo fi_s = new System.IO.FileInfo(sourceFileName);
System.IO.FileInfo fi_d = new System.IO.FileInfo(destFileName);
if (fi_d.Exists)
{
if (fi_d.LastWriteTime == fi_s.LastWriteTime) //文件已经备份了,跳过
return;
}
//他们有一样的LastWriteTime
File.Copy(sourceFileName, destFileName, true);
}
cr = CheckResult.OK;
}
/// <summary>
/// 恢复所有文件 (调取 backup 文件夹 内的文件出来)
/// </summary>
/// <returns></returns>
public bool Restore()
{
hasCheck = true;
if (!Directory.Exists("backup"))
{
cr = CheckResult.ERR_RESTORE;
return false;
}
try
{
string destFileName = Path;
string sourceFileName = @"backup\" + FileName;
if (File.Exists(sourceFileName))
{
//他们有一样的LastWriteTime
File.Copy(sourceFileName, destFileName, true);
cr = CheckResult.RESTORE;
return true;
}
else
{
cr = CheckResult.ERR_RESTORE;
return false;
}
}
catch
{
cr = CheckResult.ERR_RESTORE;
return false;
}
}
}
/// <summary>
/// 路径, 文件名
/// </summary>
static List<FileInfo> fileinfos = new List<FileInfo>();
static BackupFile()
{
}
/// <summary>
/// 加入需要备份的文件路径
/// </summary>
/// <param name="path"></param>
/// <param name="func_isFileOK"></param>
public static void Add(string path, IsFileOKHandler func_isFileOK)
{
Add(-1, path, func_isFileOK);
}
public static void Add(int groupno, string path, IsFileOKHandler func_isFileOK)
{
if ((from fi in fileinfos where fi.Path == path select fi).Count() > 0)//已经存在
return;
string filename = System.IO.Path.GetFileName(path);
string fn = filename;
//当已经有重名,在这个名字后面加数字。
int i = 1;
bool b;
while (true)
{
b = (from fi in fileinfos where fi.FileName == fn select fi).Count() > 0;
if (b == true)
{
fn = filename + i.ToString();
i++;
}
else
{
break;
}
}
fileinfos.Add(
new FileInfo()
{
groupno = groupno,
FileName = fn,
Path = path,
func_isFileOK = func_isFileOK
});
}
public static bool xml_isFileOK (string p)
{
try
{
System.Xml.Linq.XElement root = System.Xml.Linq.XElement.Load(p);
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool csv_isFileOK(string p)
{
try
{
//System.Xml.Linq.XElement root = System.Xml.Linq.XElement.Load(p);
//第1行 必须找一个逗号
using (StreamReader sw = new StreamReader(p, Encoding.GetEncoding("GB2312")))
{
while (!sw.EndOfStream)
{
string item;
item = sw.ReadLine();
if (item.Contains(','))
return true;
else
return false;
}
}
return false;
}
catch (Exception e)
{
return false;
}
}
public enum CheckResult
{
/// <summary>
/// 文件都没问题
/// </summary>
OK,
/// <summary>
/// 文件有问题,已经成功恢复
/// </summary>
RESTORE,
/// <summary>
/// 文件有问题, 恢复不成功
/// </summary>
ERR_RESTORE
}
/// <summary>
/// 检测 文件 是否完整,;完整则备份;有问题,还原
/// </summary>
/// <returns></returns>
public static CheckResult Check()
{
foreach (FileInfo fi in fileinfos)
{
if(fi.hasCheck)
continue;
//找到同组的其它fi
if (fi.groupno != -1)
{
var v = from fi1 in fileinfos where fi1.groupno == fi.groupno select fi1;
if (v.All(fi1 => (fi1.func_isFileOK != null) && (fi1.func_isFileOK(fi1.Path))))
{
//没问题,备份
foreach (FileInfo fi1 in v)
fi1.Backup();
}
else
{
//其中一个有问题, 同组的全部还原
foreach (FileInfo fi1 in v)
fi1.Restore();
}
}
else
{
if (fi.func_isFileOK(fi.Path))
{
//没问题,备份
fi.Backup();
}
else
{
//有问题, 还原
fi.Restore();
}
}
}
if (fileinfos.All(fi => fi.cr == CheckResult.OK))
return CheckResult.OK;
else if (fileinfos.Any(fi => fi.cr == CheckResult.ERR_RESTORE))
return CheckResult.ERR_RESTORE;
else
return CheckResult.RESTORE;
}
/// <summary>
/// 文件都完整, 备份 所有文件 (备份到 backup 文件夹)
/// </summary>
public static void Backup()
{
foreach (FileInfo fi in fileinfos)
{
fi.Backup();
}
}
/// <summary>
/// 恢复所有文件 (调取 backup/ 文件夹 内的文件出来)
/// </summary>
public static bool Restore()
{
foreach (FileInfo fi in fileinfos)
{
fi.Restore();
}
if (fileinfos.Any(fi => fi.cr == CheckResult.ERR_RESTORE))
return false;
else
return true;
}
}
}