FileBackupPartial.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. namespace Oreo.FileMan.Partials
  15. {
  16. public partial class FileBackupPartial : UserControl
  17. {
  18. List<string> BackupFolder = new List<string>();
  19. public FileBackupPartial()
  20. {
  21. InitializeComponent();
  22. }
  23. private void BtAddFolder_Click(object sender, EventArgs e)
  24. {
  25. FolderBrowserDialog dialog = new FolderBrowserDialog();
  26. dialog.Description = "请选择要备份的文件夹";
  27. if (dialog.ShowDialog() == DialogResult.OK)
  28. {
  29. int row = DgvPath.Rows.Count;
  30. UIEnableButton(false);
  31. string foldPath = dialog.SelectedPath;
  32. BackupFolder.Add(foldPath);
  33. UIDgvPathAdd(Path.GetFileName(foldPath));
  34. long size = 0;
  35. Task.Factory.StartNew(() =>
  36. {
  37. List<string> files = FileTool.GetAllFile(foldPath);
  38. if (ListTool.HasElements(files))
  39. {
  40. foreach (var f in files)
  41. {
  42. size += FileTool.Size(f);
  43. UIDgvPathUpdate(row, Path.GetFileName(foldPath), ByteConvertTool.Fmt(size));
  44. }
  45. }
  46. UIEnableButton(true);
  47. });
  48. }
  49. }
  50. private void BtDelFolder_Click(object sender, EventArgs e)
  51. {
  52. UIDgvPathDel();
  53. }
  54. void UIEnableButton(bool enable)
  55. {
  56. BeginInvoke(new Action(() =>
  57. {
  58. BtAddFolder.Enabled = enable;
  59. }));
  60. }
  61. void UIDgvPathAdd(string path)
  62. {
  63. BeginInvoke(new Action(() =>
  64. {
  65. DgvPath.Rows.Add(new object[] { Path.GetFileName(path), "-" });
  66. }));
  67. }
  68. void UIDgvPathDel()
  69. {
  70. BeginInvoke(new Action(() =>
  71. {
  72. if (DgvPath.CurrentRow != null)
  73. {
  74. int row = DgvPath.CurrentRow.Index;
  75. if (row >= 0)
  76. {
  77. DgvPath.Rows.RemoveAt(row);
  78. }
  79. }
  80. }));
  81. }
  82. void UIDgvPathUpdate(int row, string path, string size)
  83. {
  84. BeginInvoke(new Action(() =>
  85. {
  86. DgvPath.Rows[row].SetValues(new object[] { Path.GetFileName(path), size });
  87. }));
  88. }
  89. }
  90. }