MainForm.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Xml.Linq;
  14. using Y.Utils.DataUtils.EncryptUtils;
  15. using Y.Utils.IOUtils.FileUtils;
  16. using Y.Utils.IOUtils.PathUtils;
  17. namespace Oreo.FileMan.Views
  18. {
  19. public partial class MainForm : Form
  20. {
  21. FileCodeTool fct = new FileCodeTool();
  22. public MainForm()
  23. {
  24. InitializeComponent();
  25. }
  26. private void BtFileEncrypt_Click(object sender, EventArgs e)
  27. {
  28. string pwd = "123456789012";
  29. OpenFileDialog fileDialog = new OpenFileDialog();
  30. fileDialog.Title = "请选择要加密的文件";
  31. fileDialog.Filter = "所有文件|*.*";
  32. if (fileDialog.ShowDialog() == DialogResult.OK)
  33. {
  34. string file = fileDialog.FileName;
  35. if (File.Exists(file))
  36. {
  37. string newfile = file + ".fmk";
  38. if (!File.Exists(newfile))
  39. {
  40. Task.Factory.StartNew(() =>
  41. {
  42. int spendtime = 0;
  43. if ((spendtime = FileEncryptTool.Encrypt(file, newfile, pwd, UIProgress)) > 0)
  44. {
  45. MessageBox.Show("恭喜你,加密成功。共耗时:" + spendtime, "加密成功");
  46. }
  47. });
  48. }
  49. else
  50. {
  51. MessageBox.Show("您选择的文件已存在加密文件", "??");
  52. }
  53. }
  54. }
  55. }
  56. private void BtFileDecrypt_Click(object sender, EventArgs e)
  57. {
  58. string pwd = "123456789012";
  59. string[] fileInfo = new string[128];
  60. OpenFileDialog fileDialog = new OpenFileDialog();
  61. fileDialog.Title = "请选择要解密的文件";
  62. fileDialog.Filter = "加密文件|*.fmk";
  63. if (fileDialog.ShowDialog() == DialogResult.OK)
  64. {
  65. string file = fileDialog.FileName;
  66. if (File.Exists(file))
  67. {
  68. string newfile = file.Substring(0, file.Length - ".fmk".Length);
  69. if (!File.Exists(newfile))
  70. {
  71. Task.Factory.StartNew(() =>
  72. {
  73. int spendtime = 0;
  74. if ((spendtime = FileEncryptTool.Decrypt(file, newfile, pwd, UIProgress)) > 0)
  75. {
  76. MessageBox.Show("恭喜你,解密成功。共耗时:" + spendtime, "解密成功");
  77. }
  78. });
  79. }
  80. else
  81. {
  82. MessageBox.Show("您选择的文件已存在解密文件", "??");
  83. }
  84. }
  85. }
  86. }
  87. private void MainForm_Load(object sender, EventArgs e)
  88. {
  89. }
  90. private void button2_Click(object sender, EventArgs e)
  91. {
  92. Task.Factory.StartNew(() =>
  93. {
  94. int flag = FilePackageTool.Pack(@"D:\Temp\测试压缩\Root", @"D:\Temp\测试压缩\Root.pkg", UIProgress);
  95. if (flag > 0)
  96. MessageBox.Show("打包成功");
  97. });
  98. }
  99. private void button1_Click(object sender, EventArgs e)
  100. {
  101. Task.Factory.StartNew(() =>
  102. {
  103. int flag = FilePackageTool.Unpack(@"D:\Temp\测试压缩\Root.pkg", @"D:\Temp\测试压缩\Root", UIProgress);
  104. if (flag > 0)
  105. MessageBox.Show("拆包成功");
  106. });
  107. }
  108. private void UIProgress(long current, long total)
  109. {
  110. BeginInvoke(new Action(() =>
  111. {
  112. progressBar1.Maximum = 100;
  113. progressBar1.Value = (int)(current * 100 / total);
  114. }));
  115. }
  116. private bool CanUpdate()
  117. {
  118. string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
  119. string key = "TodayUpdateTimes";
  120. DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  121. DateTime setday = today;
  122. //读取配置
  123. string temp = CanUpdateGetConfig(file, key);
  124. if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  125. {
  126. if (setday.Hour < 3)
  127. CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
  128. else
  129. return false;
  130. }
  131. else
  132. {
  133. //配置失效,设置为默认值
  134. CanUpdateSetConfig(file, key, today.ToString());
  135. }
  136. return true;
  137. }
  138. private bool CanUpdateSetConfig(string file, string key, string value)
  139. {
  140. try
  141. {
  142. //文件不存在则创建
  143. if (!File.Exists(file + ".config"))
  144. {
  145. XElement xe = new XElement("configuration");
  146. xe.Save(file + ".config");
  147. }
  148. Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  149. if (config.AppSettings.Settings.AllKeys.Contains(key))
  150. {
  151. config.AppSettings.Settings[key].Value = value;
  152. }
  153. else
  154. {
  155. config.AppSettings.Settings.Add(key, value);
  156. }
  157. config.Save(ConfigurationSaveMode.Modified);
  158. return true;
  159. }
  160. catch (Exception e)
  161. {
  162. return false;
  163. }
  164. }
  165. private string CanUpdateGetConfig(string file, string key)
  166. {
  167. try
  168. {
  169. Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  170. if (config.AppSettings.Settings.AllKeys.Contains(key))
  171. {
  172. return config.AppSettings.Settings[key].Value;
  173. }
  174. }
  175. catch (Exception e) { }
  176. return null;
  177. }
  178. }
  179. }