Updater.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Windows;
  5. using System.Diagnostics;
  6. using System.Text.RegularExpressions;
  7. namespace MPE
  8. {
  9. static class Updater
  10. {///<summary> Выполняет проверку обновлений, если такие были нейдены предложит пользователю обновить приложение </summary>
  11. static System.Threading.Tasks.Task t;
  12. static readonly WebClient client = new WebClient();
  13. static bool fromSettings = false;
  14. public static void FindNewVersion(bool fromSettings = false)
  15. {
  16. Updater.fromSettings = fromSettings;
  17. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
  18. if (!File.Exists("updater.exe")) client.DownloadFileAsync(new Uri("https://medoo48.bitbucket.io/Download/updater.exe"), "updater.exe");
  19. client.DownloadStringCompleted += Client_DownloadStringCompleted; ;
  20. t = System.Threading.Tasks.Task.Run(() => { try { client.DownloadStringAsync(new Uri("https://medoo48.bitbucket.io/index.html")); } catch { } });
  21. }
  22. static void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  23. {
  24. try
  25. {
  26. Match version = new Regex(@"\d+\S\d+").Match(e.Result);
  27. if (version.Success)
  28. {
  29. if (Convert.ToDouble(version.Value.Replace(".", ",")) > Properties.Settings.Default.version)
  30. {// if new_version > current_version TODO...
  31. string url = "https://medoo48.bitbucket.io/Download/MPE.exe";
  32. if (MessageBox.Show($"Доступна новая версия[{version.Value}], загрузить?", "Обновление miniPosterEditor", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  33. {
  34. client.DownloadFile(new Uri(url), ($"miniPosterEditor_{version.Value}.exe"));
  35. client.Dispose();
  36. if (File.Exists("updater.exe"))
  37. {
  38. Process.Start("updater.exe", $"miniPosterEditor_{version.Value}.exe MPE.exe");
  39. Process.GetCurrentProcess().Kill();//Завершаем собственный процесс
  40. }
  41. else MessageBox.Show("Обновление не может быть проиведено, так как отсутствует файл updater.exe!");
  42. }
  43. }
  44. else if (fromSettings) { MessageBox.Show($"Обновлений нет. У Вас работает актуальная версия {Properties.Settings.Default.version}"); fromSettings = false; }
  45. }
  46. }
  47. catch (Exception ex) { }
  48. }
  49. }
  50. }