FileBackupPartial.cs 7.0 KB

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