FileBackupPartial.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using Y.Utils.IOUtils.FileUtils;
  12. using Y.Utils.DataUtils.Collections;
  13. using Y.Utils.DataUtils.UnitConvertUtils;
  14. using Y.Utils.IOUtils.PathUtils;
  15. using Oreo.FileMan.Models;
  16. using Oreo.FileMan.DatabaseEngine;
  17. using Y.Utils.IOUtils.FileManUtils;
  18. using System.Windows.Threading;
  19. using System.Threading;
  20. using Y.Utils.DataUtils.DateTimeUtils;
  21. namespace Oreo.FileMan.Partials
  22. {
  23. public partial class FileBackupPartial : UserControl
  24. {
  25. FileWatcher Watcher = new FileWatcher();
  26. string FileManBackup = @"F:\FileManBackup\";
  27. List<BackupPaths> Paths = new List<BackupPaths>();
  28. List<string> BackupFiles = new List<string>();
  29. DispatcherTimer Timer = new DispatcherTimer();
  30. int BACK_UP_INTERVAL = 5 * 1000;
  31. public FileBackupPartial()
  32. {
  33. InitializeComponent();
  34. }
  35. private void FileBackupPartial_Load(object sender, EventArgs e)
  36. {
  37. Watcher.eventHandler += WatcherChangedEvent;
  38. Watcher.Start();
  39. BackupFileTask();
  40. //读取要备份的文件路径列表
  41. Task.Factory.StartNew(() =>
  42. {
  43. using (var db = new Muse())
  44. {
  45. Paths = db.GetAll<BackupPaths>(null, false).ToList();
  46. if (ListTool.HasElements(Paths))
  47. {
  48. foreach (var b in Paths)
  49. {
  50. UIDgvPathAdd(DirTool.GetPathName(b.Path));
  51. }
  52. }
  53. }
  54. });
  55. }
  56. private void BtAddPath_Click(object sender, EventArgs e)
  57. {
  58. FolderBrowserDialog dialog = new FolderBrowserDialog();
  59. dialog.Description = "请选择要备份的文件夹";
  60. if (dialog.ShowDialog() == DialogResult.OK)
  61. {
  62. string selPath = dialog.SelectedPath;//获取选中的目录
  63. string path = DirTool.Combine(selPath, "\\");//格式化选中的目录
  64. string name = DirTool.GetPathName(selPath);//获取目录名称
  65. List<BackupPaths> clashPath = Paths.Where(x => x.Path.Contains(path) || path.Contains(x.Path)).ToList();//查询冲突项
  66. if (ListTool.HasElements(clashPath))
  67. {
  68. string cp = "";
  69. clashPath.ForEach(x => cp += (x.Path + ";"));
  70. //存在重合目录
  71. MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", path, cp));
  72. }
  73. else
  74. {
  75. UIEnableButton(false);
  76. Task.Factory.StartNew(() =>
  77. {
  78. using (var db = new Muse())
  79. {
  80. if (!db.Do<BackupPaths>().Any(x => x.Path == path))
  81. {
  82. int row = DgvPath.Rows.Count;//当前目录列表总数
  83. BackupPaths bp = new BackupPaths() { Path = path, Alias = Guid.NewGuid().ToString() };
  84. if (db.Add(bp) > 0)
  85. {
  86. Paths.Add(bp);//添加到列表
  87. UIDgvPathAdd(name);//添加到列表UI
  88. long size = 0;//目录下的文件大小
  89. List<string> files = FileTool.GetAllFile(path);
  90. if (ListTool.HasElements(files))
  91. {
  92. foreach (var f in files)
  93. {
  94. size += FileTool.Size(f);
  95. UIDgvPathUpdate(row, name, ByteConvertTool.Fmt(size));//更新目录文件大小
  96. }
  97. }
  98. }
  99. }
  100. }
  101. UIEnableButton(true);
  102. });
  103. }
  104. }
  105. }
  106. private void BtDelPath_Click(object sender, EventArgs e)
  107. {
  108. if (DgvPath.CurrentRow != null)
  109. {
  110. int row = DgvPath.CurrentRow.Index;
  111. string path = Paths[row].Path;
  112. if (row >= 0)
  113. {
  114. using (var db = new Muse())
  115. {
  116. BackupPaths bp = db.Get<BackupPaths>(x => x.Path == path, null);
  117. if (bp != null) db.Del(bp, true);
  118. Paths.RemoveAt(row);
  119. }
  120. UIDgvPathDel(row);
  121. }
  122. }
  123. }
  124. private void DgvPath_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  125. {
  126. if (e.RowIndex >= 0)
  127. {
  128. string path = Paths[e.RowIndex].Path;
  129. UIEnableButton(false);
  130. DgvFile.Rows.Clear();
  131. Task.Factory.StartNew(() =>
  132. {
  133. List<string> files = FileTool.GetAllFile(path);
  134. if (ListTool.HasElements(files))
  135. {
  136. foreach (var file in files)
  137. {
  138. UIDgvFileAdd(Path.GetFileName(file), file, FileTool.SizeFormat(file));
  139. }
  140. }
  141. UIEnableButton(true);
  142. });
  143. }
  144. }
  145. private void BtStart_Click(object sender, EventArgs e)
  146. {
  147. Watcher.Start();
  148. }
  149. private void BtStop_Click(object sender, EventArgs e)
  150. {
  151. Watcher.Stop();
  152. }
  153. private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
  154. {
  155. if (Paths.Any(x => e.FullPath.Contains(x.Path)))
  156. {
  157. //变动的是文件且文件存在
  158. if (FileTool.IsFile(e.FullPath))
  159. {
  160. //添加到备份列表
  161. if (!BackupFiles.Contains(e.FullPath)) BackupFiles.Add(e.FullPath);
  162. UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
  163. }
  164. }
  165. }
  166. private void BackupFileTask()
  167. {
  168. Task.Factory.StartNew(() =>
  169. {
  170. while (!IsDisposed)
  171. {
  172. if (ListTool.HasElements(BackupFiles))
  173. {
  174. //获取要备份的文件列表并复制样本
  175. List<string> temp;
  176. lock (BackupFiles)
  177. {
  178. temp = BackupFiles;
  179. BackupFiles = new List<string>();
  180. }
  181. foreach (var t in temp)
  182. {
  183. if (File.Exists(t))
  184. {
  185. string filepath = DirTool.GetFilePath(t);
  186. BackupPaths path = Paths.FirstOrDefault(x => filepath.Contains(x.Path));
  187. if (path != null)
  188. {
  189. string pathname = path.Path;
  190. string pathalias = path.Alias;
  191. string pathfile = t.Substring(pathname.Length, t.Length - pathname.Length);
  192. string fileext = DateTimeConvert.CompactString(DateTime.Now);
  193. string fullpath = DirTool.Combine(FileManBackup, pathalias, pathfile + "." + fileext);
  194. try
  195. {
  196. if (DirTool.Create(DirTool.GetFilePath(fullpath)))
  197. {
  198. File.Copy(t, fullpath, true);
  199. using (var db = new Muse())
  200. {
  201. db.Add(new BackupFiles()
  202. {
  203. FullPath = t,
  204. BackupFullPath = fullpath,
  205. Size = FileTool.Size(t),
  206. UpdateTime = DateTimeConvert.StandardString(DateTime.Now),
  207. });
  208. }
  209. }
  210. }
  211. catch (Exception e) { }
  212. }
  213. }
  214. }
  215. }
  216. Thread.Sleep(BACK_UP_INTERVAL);
  217. }
  218. });
  219. }
  220. /// <summary>
  221. /// 停用或启用所有按钮
  222. /// </summary>
  223. /// <param name="enable"></param>
  224. void UIEnableButton(bool enable)
  225. {
  226. BeginInvoke(new Action(() =>
  227. {
  228. BtAddPath.Enabled = enable;
  229. }));
  230. }
  231. /// <summary>
  232. /// 添加到路径列表
  233. /// </summary>
  234. /// <param name="path"></param>
  235. void UIDgvPathAdd(string name)
  236. {
  237. BeginInvoke(new Action(() =>
  238. {
  239. DgvPath.Rows.Add(new object[] { name, "-" });
  240. }));
  241. }
  242. /// <summary>
  243. /// 从路径列表删除
  244. /// </summary>
  245. void UIDgvPathDel(int row)
  246. {
  247. BeginInvoke(new Action(() =>
  248. {
  249. DgvPath.Rows.RemoveAt(row);
  250. }));
  251. }
  252. /// <summary>
  253. /// 更新到路径列表
  254. /// </summary>
  255. /// <param name="row"></param>
  256. /// <param name="path"></param>
  257. /// <param name="size"></param>
  258. void UIDgvPathUpdate(int row, string path, string size)
  259. {
  260. BeginInvoke(new Action(() =>
  261. {
  262. DgvPath.Rows[row].SetValues(new object[] { Path.GetFileName(path), size });
  263. }));
  264. }
  265. /// <summary>
  266. /// 添加文件到列表
  267. /// </summary>
  268. /// <param name="file"></param>
  269. /// <param name="path"></param>
  270. /// <param name="size"></param>
  271. void UIDgvFileAdd(string file, string path, string size)
  272. {
  273. BeginInvoke(new Action(() =>
  274. {
  275. DgvFile.Rows.Add(new object[] { file, path, size });
  276. }));
  277. }
  278. }
  279. }