FileBackupPartial.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace Oreo.FileMan.Partials
  18. {
  19. public partial class FileBackupPartial : UserControl
  20. {
  21. FileWatcher Watcher = new FileWatcher();
  22. string FileManBackup = @"D:\FileManBackup\";
  23. List<BackupPaths> Paths = new List<BackupPaths>();
  24. public FileBackupPartial()
  25. {
  26. InitializeComponent();
  27. }
  28. private void FileBackupPartial_Load(object sender, EventArgs e)
  29. {
  30. Watcher. += WatcherChangedEvent;
  31. //读取要备份的文件路径列表
  32. Task.Factory.StartNew(() =>
  33. {
  34. using (var db = new Muse())
  35. {
  36. Paths = db.GetAll<BackupPaths>(null, false).ToList();
  37. if (ListTool.HasElements(Paths))
  38. {
  39. foreach (var b in Paths)
  40. {
  41. UIDgvPathAdd(b.Name);
  42. }
  43. }
  44. }
  45. });
  46. }
  47. private void BtAddPath_Click(object sender, EventArgs e)
  48. {
  49. FolderBrowserDialog dialog = new FolderBrowserDialog();
  50. dialog.Description = "请选择要备份的文件夹";
  51. if (dialog.ShowDialog() == DialogResult.OK)
  52. {
  53. string selPath = dialog.SelectedPath;//格式化选中的目录
  54. List<BackupPaths> clashPath = Paths.Where(x => x.Path.Contains(selPath + "\\") || (selPath + "\\").Contains(x.Path)).ToList();//查询冲突项
  55. if (ListTool.HasElements(clashPath))
  56. {
  57. string cp = "";
  58. clashPath.ForEach(x => cp += (x.Path + ";"));
  59. //存在重合目录
  60. MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", selPath, cp));
  61. }
  62. else
  63. {
  64. long size = 0;//目录下的文件大小
  65. int row = DgvPath.Rows.Count;//当前目录列表总数
  66. BackupPaths bp = new BackupPaths() { Name = Path.GetFileName(selPath), Path = selPath + "\\", };
  67. Paths.Add(bp);//添加到列表
  68. UIDgvPathAdd(Path.GetFileName(selPath));//添加到列表UI
  69. UIEnableButton(false);
  70. Task.Factory.StartNew(() =>
  71. {
  72. using (var db = new Muse())
  73. {
  74. if (!db.Do<BackupPaths>().Any(x => x.Path == (selPath + "\\"))) db.Add(bp);//添加到数据库
  75. List<string> files = FileTool.GetAllFile(selPath);
  76. if (ListTool.HasElements(files))
  77. {
  78. foreach (var f in files)
  79. {
  80. size += FileTool.Size(f);
  81. UIDgvPathUpdate(row, Path.GetFileName(selPath), ByteConvertTool.Fmt(size));//更新目录文件大小
  82. }
  83. }
  84. }
  85. UIEnableButton(true);
  86. });
  87. }
  88. }
  89. }
  90. private void BtDelPath_Click(object sender, EventArgs e)
  91. {
  92. if (DgvPath.CurrentRow != null)
  93. {
  94. int row = DgvPath.CurrentRow.Index;
  95. string path = Paths[row].Path;
  96. if (row >= 0)
  97. {
  98. using (var db = new Muse())
  99. {
  100. BackupPaths bp = db.Get<BackupPaths>(x => x.Path == path, null);
  101. if (bp != null) db.Del(bp, true);
  102. Paths.RemoveAt(row);
  103. }
  104. UIDgvPathDel(row);
  105. }
  106. }
  107. }
  108. private void DgvPath_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  109. {
  110. if (e.RowIndex >= 0)
  111. {
  112. string path = Paths[e.RowIndex].Path;
  113. UIEnableButton(false);
  114. DgvFile.Rows.Clear();
  115. Task.Factory.StartNew(() =>
  116. {
  117. List<string> files = FileTool.GetAllFile(path);
  118. if (ListTool.HasElements(files))
  119. {
  120. foreach (var file in files)
  121. {
  122. UIDgvFileAdd(Path.GetFileName(file), file, FileTool.SizeFormat(file));
  123. }
  124. }
  125. UIEnableButton(true);
  126. });
  127. }
  128. }
  129. private void BtStart_Click(object sender, EventArgs e)
  130. {
  131. Watcher.Start();
  132. }
  133. private void BtStop_Click(object sender, EventArgs e)
  134. {
  135. Watcher.Stop();
  136. }
  137. private void WatcherChangedEvent(object sender, FileSystemEventArgs e)
  138. {
  139. UIDgvFileAdd(e.Name, e.FullPath, "Changed");
  140. }
  141. /// <summary>
  142. /// 停用或启用所有按钮
  143. /// </summary>
  144. /// <param name="enable"></param>
  145. void UIEnableButton(bool enable)
  146. {
  147. BeginInvoke(new Action(() =>
  148. {
  149. BtAddPath.Enabled = enable;
  150. }));
  151. }
  152. /// <summary>
  153. /// 添加到路径列表
  154. /// </summary>
  155. /// <param name="path"></param>
  156. void UIDgvPathAdd(string name)
  157. {
  158. BeginInvoke(new Action(() =>
  159. {
  160. DgvPath.Rows.Add(new object[] { name, "-" });
  161. }));
  162. }
  163. /// <summary>
  164. /// 从路径列表删除
  165. /// </summary>
  166. void UIDgvPathDel(int row)
  167. {
  168. BeginInvoke(new Action(() =>
  169. {
  170. DgvPath.Rows.RemoveAt(row);
  171. }));
  172. }
  173. /// <summary>
  174. /// 更新到路径列表
  175. /// </summary>
  176. /// <param name="row"></param>
  177. /// <param name="path"></param>
  178. /// <param name="size"></param>
  179. void UIDgvPathUpdate(int row, string path, string size)
  180. {
  181. BeginInvoke(new Action(() =>
  182. {
  183. DgvPath.Rows[row].SetValues(new object[] { Path.GetFileName(path), size });
  184. }));
  185. }
  186. /// <summary>
  187. /// 添加文件到列表
  188. /// </summary>
  189. /// <param name="file"></param>
  190. /// <param name="path"></param>
  191. /// <param name="size"></param>
  192. void UIDgvFileAdd(string file, string path, string size)
  193. {
  194. BeginInvoke(new Action(() =>
  195. {
  196. DgvFile.Rows.Add(new object[] { file, path, size });
  197. }));
  198. }
  199. }
  200. }