MainForm.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Y.Utils.DataUtils.EncryptUtils;
  14. using Y.Utils.IOUtils.FileUtils;
  15. using Y.Utils.IOUtils.PathUtils;
  16. namespace Oreo.FileMan.Views
  17. {
  18. public partial class MainForm : Form
  19. {
  20. FileCodeTool fct = new FileCodeTool();
  21. public MainForm()
  22. {
  23. InitializeComponent();
  24. }
  25. private void BtFileEncrypt_Click(object sender, EventArgs e)
  26. {
  27. string pwd = "123456789012";
  28. OpenFileDialog fileDialog = new OpenFileDialog();
  29. fileDialog.Title = "请选择要加密的文件";
  30. fileDialog.Filter = "所有文件|*.*";
  31. if (fileDialog.ShowDialog() == DialogResult.OK)
  32. {
  33. string file = fileDialog.FileName;
  34. if (File.Exists(file))
  35. {
  36. string newfile = file + ".fmk";
  37. if (!File.Exists(newfile))
  38. {
  39. int spendtime = 0;
  40. if ((spendtime = FileEncryptTool.Encrypt(file, newfile, pwd)) > 0)
  41. {
  42. MessageBox.Show("恭喜你,加密成功。共耗时:" + spendtime, "加密成功");
  43. }
  44. }
  45. else
  46. {
  47. MessageBox.Show("您选择的文件已存在加密文件", "??");
  48. }
  49. }
  50. }
  51. }
  52. private void BtFileDecrypt_Click(object sender, EventArgs e)
  53. {
  54. string pwd = "123456789012";
  55. string[] fileInfo = new string[128];
  56. OpenFileDialog fileDialog = new OpenFileDialog();
  57. fileDialog.Title = "请选择要解密的文件";
  58. fileDialog.Filter = "加密文件|*.fmk";
  59. if (fileDialog.ShowDialog() == DialogResult.OK)
  60. {
  61. string file = fileDialog.FileName;
  62. if (File.Exists(file))
  63. {
  64. string newfile = file.Substring(0, file.Length - ".fmk".Length);
  65. if (!File.Exists(newfile))
  66. {
  67. int spendtime = 0;
  68. if ((spendtime = FileEncryptTool.Decrypt(file, newfile, pwd)) > 0)
  69. {
  70. MessageBox.Show("恭喜你,解密成功。共耗时:" + spendtime, "解密成功");
  71. }
  72. }
  73. else
  74. {
  75. MessageBox.Show("您选择的文件已存在解密文件", "??");
  76. }
  77. }
  78. }
  79. }
  80. private void MainForm_Load(object sender, EventArgs e)
  81. {
  82. bool flag = CanUpdate();
  83. }
  84. private void button2_Click(object sender, EventArgs e)
  85. {
  86. FilePackageTool.Pack(@"D:\Temp\测试压缩\Root", @"D:\Temp\测试压缩\Root.pkg");
  87. }
  88. private void button1_Click(object sender, EventArgs e)
  89. {
  90. FilePackageTool.Unpack(@"D:\Temp\测试压缩\Root.pkg", @"D:\Temp\测试压缩\Root");
  91. }
  92. private bool CanUpdate()
  93. {
  94. string key = "TodayUpdateTimes";
  95. DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  96. DateTime setday = today;
  97. //读取配置
  98. string temp = ConfigurationManager.AppSettings[key];
  99. if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  100. {
  101. if (setday.Hour < 5)
  102. CanUpdateSetConfig(key, setday.AddHours(1).ToString());//累加hour记录次数
  103. else
  104. return false;
  105. }
  106. else
  107. {
  108. //配置失效,设置为默认值
  109. CanUpdateSetConfig(key, today.ToString());
  110. }
  111. return true;
  112. }
  113. private bool CanUpdateSetConfig(string key, string value)
  114. {
  115. try
  116. {
  117. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  118. if (config.AppSettings.Settings.AllKeys.Contains(key))
  119. {
  120. config.AppSettings.Settings[key].Value = value;
  121. }
  122. else
  123. {
  124. config.AppSettings.Settings.Add(key, value);
  125. }
  126. config.Save(ConfigurationSaveMode.Modified);
  127. ConfigurationManager.RefreshSection("appSettings");
  128. return true;
  129. }
  130. catch
  131. {
  132. return false;
  133. }
  134. }
  135. }
  136. }