FileBackupPartial.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = @"G:\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. BackupFileTask();
  39. //读取要备份的文件路径列表
  40. Task.Factory.StartNew(() =>
  41. {
  42. using (var db = new Muse())
  43. {
  44. Paths = db.GetAll<BackupPaths>(null, false).ToList();
  45. if (ListTool.HasElements(Paths))
  46. {
  47. foreach (var p in Paths)
  48. {
  49. Watcher.Add(p.Path, true);
  50. UIDgvPathAdd(DirTool.GetPathName(p.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. Watcher.Add(bp.Path, true);//添加到监听
  88. UIDgvPathAdd(name);//添加到列表UI
  89. long size = 0;//目录下的文件大小
  90. List<string> files = FileTool.GetAllFile(path);
  91. if (ListTool.HasElements(files))
  92. {
  93. foreach (var f in files)
  94. {
  95. size += FileTool.Size(f);
  96. UIDgvPathUpdate(row, name, ByteConvertTool.Fmt(size));//更新目录文件大小
  97. }
  98. }
  99. }
  100. }
  101. }
  102. UIEnableButton(true);
  103. });
  104. }
  105. }
  106. }
  107. private void BtDelPath_Click(object sender, EventArgs e)
  108. {
  109. if (DgvPath.CurrentRow != null)
  110. {
  111. int row = DgvPath.CurrentRow.Index;
  112. string path = Paths[row].Path;
  113. if (row >= 0)
  114. {
  115. using (var db = new Muse())
  116. {
  117. BackupPaths bp = db.Get<BackupPaths>(x => x.Path == path, null);
  118. if (bp != null) db.Del(bp, true);
  119. Paths.RemoveAt(row);
  120. }
  121. UIDgvPathDel(row);
  122. }
  123. }
  124. }
  125. private void DgvPath_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  126. {
  127. if (e.RowIndex >= 0)
  128. {
  129. string path = Paths[e.RowIndex].Path;
  130. UIEnableButton(false);
  131. DgvFile.Rows.Clear();
  132. Task.Factory.StartNew(() =>
  133. {
  134. List<string> files = FileTool.GetAllFile(path);
  135. if (ListTool.HasElements(files))
  136. {
  137. foreach (var file in files)
  138. {
  139. UIDgvFileAdd(Path.GetFileName(file), file, FileTool.SizeFormat(file));
  140. }
  141. }
  142. UIEnableButton(true);
  143. });
  144. }
  145. }
  146. private void WatcherChangedEvent(object sender, FileWatcherEventArgs e)
  147. {
  148. if (Paths.Any(x => e.FullPath.Contains(x.Path)))
  149. {
  150. //变动的是文件且文件存在
  151. if (FileTool.IsFile(e.FullPath))
  152. {
  153. //添加到备份列表
  154. if (!BackupFiles.Contains(e.FullPath)) BackupFiles.Add(e.FullPath);
  155. UIDgvFileAdd(e.Name, e.FullPath, e.ChangeType.ToString());
  156. }
  157. }
  158. }
  159. private void BackupFileTask()
  160. {
  161. Task.Factory.StartNew(() =>
  162. {
  163. while (!IsDisposed)
  164. {
  165. if (ListTool.HasElements(BackupFiles))
  166. {
  167. //获取要备份的文件列表并复制样本
  168. List<string> temp;
  169. lock (BackupFiles)
  170. {
  171. temp = BackupFiles;
  172. BackupFiles = new List<string>();
  173. }
  174. foreach (var t in temp)
  175. {
  176. if (File.Exists(t))
  177. {
  178. string filepath = DirTool.GetFilePath(t);
  179. BackupPaths path = Paths.FirstOrDefault(x => filepath.Contains(x.Path));
  180. if (path != null)
  181. {
  182. string pathname = path.Path;
  183. string pathalias = path.Alias;
  184. string pathfile = t.Substring(pathname.Length, t.Length - pathname.Length);
  185. string fileext = DateTimeConvert.CompactString(DateTime.Now);
  186. string fullpath = DirTool.Combine(FileManBackup, pathalias, pathfile + "." + fileext);
  187. try
  188. {
  189. if (DirTool.Create(DirTool.GetFilePath(fullpath)))
  190. {
  191. File.Copy(t, fullpath, true);
  192. using (var db = new Muse())
  193. {
  194. db.Add(new BackupFiles()
  195. {
  196. FullPath = t,
  197. BackupFullPath = fullpath,
  198. Size = FileTool.Size(t),
  199. UpdateTime = DateTimeConvert.StandardString(DateTime.Now),
  200. });
  201. }
  202. }
  203. }
  204. catch (Exception e) { }
  205. }
  206. }
  207. }
  208. }
  209. Thread.Sleep(BACK_UP_INTERVAL);
  210. }
  211. });
  212. }
  213. /// <summary>
  214. /// 停用或启用所有按钮
  215. /// </summary>
  216. /// <param name="enable"></param>
  217. void UIEnableButton(bool enable)
  218. {
  219. BeginInvoke(new Action(() =>
  220. {
  221. BtAddPath.Enabled = enable;
  222. }));
  223. }
  224. /// <summary>
  225. /// 添加到路径列表
  226. /// </summary>
  227. /// <param name="path"></param>
  228. void UIDgvPathAdd(string name)
  229. {
  230. BeginInvoke(new Action(() =>
  231. {
  232. DgvPath.Rows.Add(new object[] { name, "-" });
  233. }));
  234. }
  235. /// <summary>
  236. /// 从路径列表删除
  237. /// </summary>
  238. void UIDgvPathDel(int row)
  239. {
  240. BeginInvoke(new Action(() =>
  241. {
  242. DgvPath.Rows.RemoveAt(row);
  243. }));
  244. }
  245. /// <summary>
  246. /// 更新到路径列表
  247. /// </summary>
  248. /// <param name="row"></param>
  249. /// <param name="path"></param>
  250. /// <param name="size"></param>
  251. void UIDgvPathUpdate(int row, string path, string size)
  252. {
  253. BeginInvoke(new Action(() =>
  254. {
  255. DgvPath.Rows[row].SetValues(new object[] { Path.GetFileName(path), size });
  256. }));
  257. }
  258. /// <summary>
  259. /// 添加文件到列表
  260. /// </summary>
  261. /// <param name="file"></param>
  262. /// <param name="path"></param>
  263. /// <param name="size"></param>
  264. void UIDgvFileAdd(string file, string path, string size)
  265. {
  266. BeginInvoke(new Action(() =>
  267. {
  268. DgvFile.Rows.Add(new object[] { file, path, size });
  269. }));
  270. }
  271. }
  272. }