FileDecryptPartial.cs 5.2 KB

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