FileBackupPartial.cs 6.7 KB

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