MainForm.cs 7.0 KB

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