MainForm.cs 7.0 KB

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