FileBackupPartial.cs 10 KB

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