FileBackupPartial.cs 7.5 KB

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