MainForm.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. int flag = FilePackageTool.Pack(@"D:\temp\测试打包\新建文件夹", @"D:\temp\测试打包\新建文件夹.pkg");
  87. if (flag > 0)
  88. MessageBox.Show("打包成功");
  89. }
  90. private void button1_Click(object sender, EventArgs e)
  91. {
  92. int flag = FilePackageTool.Unpack(@"D:\temp\测试打包\新建文件夹.pkg", @"D:\temp\测试打包\新建文件夹");
  93. if (flag > 0)
  94. MessageBox.Show("拆包成功");
  95. }
  96. private bool CanUpdate()
  97. {
  98. string key = "TodayUpdateTimes";
  99. DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  100. DateTime setday = today;
  101. //读取配置
  102. string temp = ConfigurationManager.AppSettings[key];
  103. if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  104. {
  105. if (setday.Hour < 3)
  106. CanUpdateSetConfig(key, setday.AddHours(1).ToString());//累加hour记录次数
  107. else
  108. return false;
  109. }
  110. else
  111. {
  112. //配置失效,设置为默认值
  113. CanUpdateSetConfig(key, today.ToString());
  114. }
  115. return true;
  116. }
  117. private bool CanUpdateSetConfig(string key, string value)
  118. {
  119. try
  120. {
  121. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  122. if (config.AppSettings.Settings.AllKeys.Contains(key))
  123. {
  124. config.AppSettings.Settings[key].Value = value;
  125. }
  126. else
  127. {
  128. config.AppSettings.Settings.Add(key, value);
  129. }
  130. config.Save(ConfigurationSaveMode.Modified);
  131. ConfigurationManager.RefreshSection("appSettings");
  132. return true;
  133. }
  134. catch
  135. {
  136. return false;
  137. }
  138. }
  139. }
  140. }