MainForm.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //设置选项卡控件标签栏高度为1
  94. tabControl1.SizeMode = TabSizeMode.Fixed;
  95. tabControl1.ItemSize = new Size(0, 1);
  96. }
  97. private void button2_Click(object sender, EventArgs e)
  98. {
  99. Task.Factory.StartNew(() =>
  100. {
  101. int flag = FilePackageTool.Pack(@"D:\temp\测试打包\新建文件夹", @"D:\temp\测试打包\新建文件夹.pkg", UIProgress);
  102. if (flag > 0)
  103. MessageBox.Show("打包成功");
  104. });
  105. }
  106. private void button1_Click(object sender, EventArgs e)
  107. {
  108. Task.Factory.StartNew(() =>
  109. {
  110. int flag = FilePackageTool.Unpack(@"D:\temp\测试打包\新建文件夹.pkg", @"D:\temp\测试打包\新建文件夹", UIProgress);
  111. if (flag > 0)
  112. MessageBox.Show("拆包成功");
  113. });
  114. }
  115. private void UIProgress(long current, long total)
  116. {
  117. BeginInvoke(new Action(() =>
  118. {
  119. progressBar1.Maximum = 100;
  120. progressBar1.Value = (int)(current * 100 / total);
  121. label1.Text = current + "/" + total;
  122. }));
  123. }
  124. private bool CanUpdate()
  125. {
  126. string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
  127. string key = "TodayUpdateTimes";
  128. DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  129. DateTime setday = today;
  130. //读取配置
  131. string temp = CanUpdateGetConfig(file, key);
  132. if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  133. {
  134. if (setday.Hour < 3)
  135. CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
  136. else
  137. return false;
  138. }
  139. else
  140. {
  141. //配置失效,设置为默认值
  142. CanUpdateSetConfig(file, key, today.ToString());
  143. }
  144. return true;
  145. }
  146. private bool CanUpdateSetConfig(string file, string key, string value)
  147. {
  148. try
  149. {
  150. //文件不存在则创建
  151. if (!File.Exists(file + ".config"))
  152. {
  153. XElement xe = new XElement("configuration");
  154. xe.Save(file + ".config");
  155. }
  156. Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  157. if (config.AppSettings.Settings.AllKeys.Contains(key))
  158. {
  159. config.AppSettings.Settings[key].Value = value;
  160. }
  161. else
  162. {
  163. config.AppSettings.Settings.Add(key, value);
  164. }
  165. config.Save(ConfigurationSaveMode.Modified);
  166. return true;
  167. }
  168. catch (Exception e)
  169. {
  170. return false;
  171. }
  172. }
  173. private string CanUpdateGetConfig(string file, string key)
  174. {
  175. try
  176. {
  177. Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  178. if (config.AppSettings.Settings.AllKeys.Contains(key))
  179. {
  180. return config.AppSettings.Settings[key].Value;
  181. }
  182. }
  183. catch (Exception e) { }
  184. return null;
  185. }
  186. private void imageButton1_Click(object sender, EventArgs e)
  187. {
  188. //MessageBox.Show("");
  189. }
  190. private void imageButton4_Click(object sender, EventArgs e)
  191. {
  192. }
  193. private void LbFileType_Click(object sender, EventArgs e)
  194. {
  195. tabControl1.SelectTab("TpFileType");
  196. }
  197. private void LbFileEncrypt_Click(object sender, EventArgs e)
  198. {
  199. tabControl1.SelectTab("TpFileEncrypt");
  200. }
  201. private void LbFileBackup_Click(object sender, EventArgs e)
  202. {
  203. tabControl1.SelectTab("TpFileBackup");
  204. }
  205. }
  206. }