UpdateInstaller.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 static string UpdateFilesPath = Path.Join(UpdateDownloader.DownloadLocation, TargetDirectoryName);
  11. public event EventHandler<UpdateProgressChangedEventArgs> ProgressChanged;
  12. private float _progress = 0;
  13. public float Progress
  14. {
  15. get => _progress;
  16. set
  17. {
  18. _progress = value;
  19. ProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(value));
  20. }
  21. }
  22. public string ArchiveFileName { get; set; }
  23. public string TargetDirectory { get; set; }
  24. public UpdateInstaller(string archiveFileName, string targetDirectory)
  25. {
  26. ArchiveFileName = archiveFileName;
  27. TargetDirectory = targetDirectory;
  28. }
  29. public void Install()
  30. {
  31. var processes = Process.GetProcessesByName("PixiEditor");
  32. if (processes.Length > 0)
  33. {
  34. processes[0].WaitForExit();
  35. }
  36. ZipFile.ExtractToDirectory(ArchiveFileName, UpdateFilesPath, true);
  37. Progress = 25; //25% for unzip
  38. string dirWithFiles = Directory.GetDirectories(UpdateFilesPath)[0];
  39. string[] files = Directory.GetFiles(dirWithFiles);
  40. CopyFilesToDestination(files);
  41. DeleteArchive();
  42. Progress = 100;
  43. }
  44. private void DeleteArchive()
  45. {
  46. File.Delete(ArchiveFileName);
  47. Directory.Delete(UpdateFilesPath, true);
  48. }
  49. private void CopyFilesToDestination(string[] files)
  50. {
  51. float fileCopiedVal = 74f / files.Length; //74% is reserved for copying
  52. string destinationDir = TargetDirectory;
  53. foreach (string file in files)
  54. {
  55. string targetFileName = Path.GetFileName(file);
  56. File.Copy(file, Path.Join(destinationDir, targetFileName), true);
  57. Progress += fileCopiedVal;
  58. }
  59. }
  60. }
  61. }