UpdateInstaller.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Compression;
  5. namespace PixiEditor.UpdateModule
  6. {
  7. public class UpdateInstaller
  8. {
  9. public const string TargetDirectoryName = "UpdateFiles";
  10. public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
  11. private float _progress = 0;
  12. public float Progress
  13. {
  14. get => _progress;
  15. set
  16. {
  17. _progress = value;
  18. ProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(value));
  19. }
  20. }
  21. public string ArchiveFileName { get; set; }
  22. public UpdateInstaller(string archiveFileName)
  23. {
  24. ArchiveFileName = archiveFileName;
  25. }
  26. public void Install()
  27. {
  28. var processes = Process.GetProcessesByName("PixiEditor");
  29. if(processes.Length > 0)
  30. {
  31. processes[0].WaitForExit();
  32. }
  33. ZipFile.ExtractToDirectory(ArchiveFileName, TargetDirectoryName, true);
  34. Progress = 25; //25% for unzip
  35. string dirWithFiles = Directory.GetDirectories(TargetDirectoryName)[0];
  36. string[] files = Directory.GetFiles(dirWithFiles);
  37. CopyFilesToDestination(files);
  38. DeleteArchive();
  39. Progress = 100;
  40. }
  41. private void DeleteArchive()
  42. {
  43. File.Delete(ArchiveFileName);
  44. }
  45. private void CopyFilesToDestination(string[] files)
  46. {
  47. float fileCopiedVal = 74f / files.Length; //74% is reserved for copying
  48. string destinationDir = Path.GetDirectoryName(ArchiveFileName);
  49. foreach (string file in files)
  50. {
  51. string targetFileName = Path.GetFileName(file);
  52. File.Copy(file, Path.Join(destinationDir, targetFileName), true);
  53. Progress += fileCopiedVal;
  54. }
  55. }
  56. }
  57. }