App.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Configuration;
  2. using System.Data;
  3. using System.IO.Compression;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Windows;
  8. namespace ArchivesCenter3
  9. {
  10. /// <summary>
  11. /// Interaction logic for App.xaml
  12. /// </summary>
  13. public partial class App : Application
  14. {
  15. protected override void OnStartup(StartupEventArgs e)
  16. {
  17. base.OnStartup(e);
  18. this.SessionEnding += App_SessionEnding;
  19. }
  20. private void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
  21. {
  22. try
  23. {
  24. // 获取用户文档库路径
  25. string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  26. string archivesCenterPath = Path.Combine(documentsPath, "ArchivesCenter");
  27. string dataPath = Path.Combine(archivesCenterPath, "data");
  28. string configPath = Path.Combine(archivesCenterPath, "config");
  29. string backupPath = Path.Combine(archivesCenterPath, "backup");
  30. // 确保备份文件夹存在
  31. Directory.CreateDirectory(backupPath);
  32. // 获取当前时间作为文件名
  33. string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  34. string backupFileName = Path.Combine(backupPath, $"backup_{timestamp}.zip");
  35. // 获取加密密码
  36. string password = Settings1.Default.Password;
  37. // 创建压缩包并加密
  38. using (FileStream zipToOpen = new FileStream(backupFileName, FileMode.Create))
  39. {
  40. using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create, true))
  41. {
  42. // 添加 data 文件夹
  43. if (Directory.Exists(dataPath))
  44. {
  45. AddDirectoryToArchive(archive, dataPath, "data");
  46. }
  47. // 添加 config 文件夹
  48. if (Directory.Exists(configPath))
  49. {
  50. AddDirectoryToArchive(archive, configPath, "config");
  51. }
  52. }
  53. }
  54. // 加密压缩包
  55. EncryptFile(backupFileName, backupFileName + ".enc", password);
  56. // 删除未加密的压缩包
  57. File.Delete(backupFileName);
  58. MessageBox.Show("备份完成,文件已加密并保存到:\n" + backupFileName + ".enc");
  59. }
  60. catch (Exception ex)
  61. {
  62. MessageBox.Show("备份失败:" + ex.Message);
  63. }
  64. }
  65. private void AddDirectoryToArchive(ZipArchive archive, string directoryPath, string entryName)
  66. {
  67. foreach (string file in Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories))
  68. {
  69. string entryFilePath = Path.GetRelativePath(directoryPath, file);
  70. ZipArchiveEntry entry = archive.CreateEntry(Path.Combine(entryName, entryFilePath));
  71. using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
  72. {
  73. using (Stream entryStream = entry.Open())
  74. {
  75. fileStream.CopyTo(entryStream);
  76. }
  77. }
  78. }
  79. }
  80. private void EncryptFile(string inputFilePath, string outputFilePath, string password)
  81. {
  82. byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  83. using (Aes aesAlg = Aes.Create())
  84. {
  85. aesAlg.Key = passwordBytes;
  86. aesAlg.IV = new byte[16]; // IV 长度必须为 16 字节
  87. using (FileStream inputStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
  88. {
  89. using (FileStream outputStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
  90. {
  91. using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV))
  92. {
  93. using (CryptoStream cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
  94. {
  95. inputStream.CopyTo(cryptoStream);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }