FileEncryptPartial.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Y.Utils.IOUtils.FileUtils;
  11. using Y.Utils.DataUtils.Collections;
  12. using Y.Utils.DelegateUtils;
  13. namespace Oreo.FileMan.Partials
  14. {
  15. public partial class FileEncryptPartial : UserControl
  16. {
  17. public FileEncryptPartial()
  18. {
  19. InitializeComponent();
  20. }
  21. /// <summary>
  22. /// 文件批量加密
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="e"></param>
  26. private void BtFileEncrypt_Click(object sender, EventArgs e)
  27. {
  28. ((Button)sender).Enabled = false;
  29. string password = TbFileEncryptPassword.Text;
  30. Task.Factory.StartNew(() =>
  31. {
  32. int index = 0;
  33. foreach (DataGridViewRow row in DgvFileEncryptList.Rows)
  34. {
  35. string file = row.Cells["CoFileEncryptName"].Value.ToString();
  36. int flag;
  37. UIFileEncryptStatus(index, "分析文件...");
  38. if ((flag = FileEncryptTool.Encrypt(file, file + FileEncryptTool.FileExt, password, UIFileEncryptProgress, index)) > 0)
  39. {
  40. UIFileEncryptStatus(index, "成功");
  41. if (CbFileEncryptDelete.Checked)
  42. {
  43. FileTool.Delete(file);
  44. }
  45. }
  46. else
  47. {
  48. UIFileEncryptStatus(index, "失败:" + flag);
  49. }
  50. index++;
  51. }
  52. BeginInvoke(new Action(() => { ((Button)sender).Enabled = true; }));
  53. });
  54. }
  55. /// <summary>
  56. /// 添加文件
  57. /// </summary>
  58. /// <param name="sender"></param>
  59. /// <param name="e"></param>
  60. private void BtFileEncryptAdd_Click(object sender, EventArgs e)
  61. {
  62. OpenFileDialog fileDialog = new OpenFileDialog();
  63. fileDialog.Title = "请选择要加密的文件";
  64. fileDialog.Filter = "所有文件|*.*";
  65. fileDialog.Multiselect = true;
  66. if (fileDialog.ShowDialog() == DialogResult.OK)
  67. {
  68. if (ListTool.HasElements(fileDialog.FileNames))
  69. foreach (var file in fileDialog.FileNames)
  70. {
  71. DgvFileEncryptList.Rows.Add(new object[] { file, "准备" });
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 批量导入文件
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private void BtFileEncryptAdds_Click(object sender, EventArgs e)
  81. {
  82. FolderBrowserDialog dialog = new FolderBrowserDialog();
  83. dialog.Description = "请选择要导入的文件路径";
  84. if (dialog.ShowDialog() == DialogResult.OK)
  85. {
  86. string foldPath = dialog.SelectedPath;
  87. List<string> fileList = FileTool.GetAllFile(foldPath);
  88. if (ListTool.HasElements(fileList))
  89. {
  90. fileList.ForEach(x =>
  91. {
  92. DgvFileEncryptList.Rows.Add(new object[] { x, "准备" });
  93. });
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// 清空文件列表
  99. /// </summary>
  100. /// <param name="sender"></param>
  101. /// <param name="e"></param>
  102. private void BtFileEncryptClear_Click(object sender, EventArgs e)
  103. {
  104. DgvFileEncryptList.Rows.Clear();
  105. }
  106. /// <summary>
  107. /// 显示当前加密的文件进度
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void UIFileEncryptProgress(object sender, ProgressEventArgs e)
  112. {
  113. BeginInvoke(new Action(() =>
  114. {
  115. DgvFileEncryptList.Rows[(int)sender].Cells["CoFileEncryptStatus"].Value =
  116. (e.Current * 100 / e.Total) + " %";
  117. }));
  118. }
  119. /// <summary>
  120. /// 更新文件状态
  121. /// </summary>
  122. /// <param name="index"></param>
  123. /// <param name="status"></param>
  124. private void UIFileEncryptStatus(int index, string status)
  125. {
  126. BeginInvoke(new Action(() =>
  127. {
  128. DgvFileEncryptList.Rows[index].Cells["CoFileEncryptStatus"].Value = status;
  129. }));
  130. }
  131. }
  132. }