| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml.Linq;
- using Y.Utils.DataUtils.EncryptUtils;
- using Y.Utils.IOUtils.FileUtils;
- using Y.Utils.IOUtils.PathUtils;
- namespace Oreo.FileMan.Views
- {
- public partial class MainForm : Form
- {
- FileCodeTool fct = new FileCodeTool();
- public MainForm()
- {
- InitializeComponent();
- }
- private void BtFileEncrypt_Click(object sender, EventArgs e)
- {
- string pwd = "123456789012";
- OpenFileDialog fileDialog = new OpenFileDialog();
- fileDialog.Title = "请选择要加密的文件";
- fileDialog.Filter = "所有文件|*.*";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- string file = fileDialog.FileName;
- if (File.Exists(file))
- {
- string newfile = file + ".fmk";
- if (!File.Exists(newfile))
- {
- Task.Factory.StartNew(() =>
- {
- int spendtime = 0;
- if ((spendtime = FileEncryptTool.Encrypt(file, newfile, pwd, UIProgress)) > 0)
- {
- MessageBox.Show("恭喜你,加密成功。共耗时:" + spendtime, "加密成功");
- }
- });
- }
- else
- {
- MessageBox.Show("您选择的文件已存在加密文件", "??");
- }
- }
- }
- }
- private void BtFileDecrypt_Click(object sender, EventArgs e)
- {
- string pwd = "123456789012";
- string[] fileInfo = new string[128];
- OpenFileDialog fileDialog = new OpenFileDialog();
- fileDialog.Title = "请选择要解密的文件";
- fileDialog.Filter = "加密文件|*.fmk";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- string file = fileDialog.FileName;
- if (File.Exists(file))
- {
- string newfile = file.Substring(0, file.Length - ".fmk".Length);
- if (!File.Exists(newfile))
- {
- Task.Factory.StartNew(() =>
- {
- int spendtime = 0;
- if ((spendtime = FileEncryptTool.Decrypt(file, newfile, pwd, UIProgress)) > 0)
- {
- MessageBox.Show("恭喜你,解密成功。共耗时:" + spendtime, "解密成功");
- }
- });
- }
- else
- {
- MessageBox.Show("您选择的文件已存在解密文件", "??");
- }
- }
- }
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Task.Factory.StartNew(() =>
- {
- int flag = FilePackageTool.Pack(@"D:\Temp\测试压缩\Root", @"D:\Temp\测试压缩\Root.pkg", UIProgress);
- if (flag > 0)
- MessageBox.Show("打包成功");
- });
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Task.Factory.StartNew(() =>
- {
- int flag = FilePackageTool.Unpack(@"D:\Temp\测试压缩\Root.pkg", @"D:\Temp\测试压缩\Root", UIProgress);
- if (flag > 0)
- MessageBox.Show("拆包成功");
- });
- }
- private void UIProgress(long current, long total)
- {
- BeginInvoke(new Action(() =>
- {
- progressBar1.Maximum = 100;
- progressBar1.Value = (int)(current * 100 / total);
- }));
- }
- private bool CanUpdate()
- {
- string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
- string key = "TodayUpdateTimes";
- DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
- DateTime setday = today;
- //读取配置
- string temp = CanUpdateGetConfig(file, key);
- if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
- {
- if (setday.Hour < 3)
- CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
- else
- return false;
- }
- else
- {
- //配置失效,设置为默认值
- CanUpdateSetConfig(file, key, today.ToString());
- }
- return true;
- }
- private bool CanUpdateSetConfig(string file, string key, string value)
- {
- try
- {
- //文件不存在则创建
- if (!File.Exists(file + ".config"))
- {
- XElement xe = new XElement("configuration");
- xe.Save(file + ".config");
- }
- Configuration config = ConfigurationManager.OpenExeConfiguration(file);
- if (config.AppSettings.Settings.AllKeys.Contains(key))
- {
- config.AppSettings.Settings[key].Value = value;
- }
- else
- {
- config.AppSettings.Settings.Add(key, value);
- }
- config.Save(ConfigurationSaveMode.Modified);
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
- private string CanUpdateGetConfig(string file, string key)
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(file);
- if (config.AppSettings.Settings.AllKeys.Contains(key))
- {
- return config.AppSettings.Settings[key].Value;
- }
- }
- catch (Exception e) { }
- return null;
- }
- }
- }
|