WndUpdateAsk.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using MeterVision.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. namespace MeterVision.Upgrade
  20. {
  21. /// <summary>
  22. /// WndUpdateAsk.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class WndUpdateAsk : Window
  25. {
  26. private UpgradeModel UpgradeModel;
  27. private WebClient downWebClient = new WebClient();
  28. public WndUpdateAsk(UpgradeModel upgradeModel)
  29. {
  30. InitializeComponent();
  31. UpgradeModel = upgradeModel;
  32. txtNewVersion.Text = $"发现新版本 V{upgradeModel.VersionCode}";
  33. txtInfo.Text = upgradeModel.Updates;
  34. }
  35. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  36. {
  37. if(e.LeftButton == MouseButtonState.Pressed)
  38. {
  39. DragMove();
  40. }
  41. }
  42. private void BtnIgnore_Click(object sender, RoutedEventArgs e)
  43. {
  44. this.Close();
  45. }
  46. private void BtnUpdate_Click(object sender, RoutedEventArgs e)
  47. {
  48. // 设置下载进度事件
  49. downWebClient.DownloadProgressChanged += DownWebClient_DownloadProgressChanged;
  50. downWebClient.DownloadFileCompleted += DownWebClient_DownloadFileCompleted;
  51. string srcdownFile = Path.Combine(UpgradeHelper.UpgradeUrl, UpgradeModel.ApkName);
  52. string localFileFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "upgrade");
  53. // 确保文件夹存在
  54. if (!Directory.Exists(localFileFolderPath))
  55. {
  56. Directory.CreateDirectory(localFileFolderPath);
  57. }
  58. string desdownFile = Path.Combine(localFileFolderPath, UpgradeModel.ApkName);
  59. try
  60. {
  61. pnlUpdate.Visibility = Visibility.Collapsed;
  62. pnlProgress.Visibility = Visibility.Visible;
  63. downWebClient.DownloadFileAsync(new Uri(srcdownFile), desdownFile,desdownFile);
  64. }
  65. catch(WebException ex)
  66. {
  67. MessageBox.Show(ex.Message);
  68. pnlUpdate.Visibility = Visibility.Visible;
  69. pnlProgress.Visibility = Visibility.Collapsed;
  70. }
  71. catch (Exception ex)
  72. {
  73. MessageBox.Show($"发生意外错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  74. pnlUpdate.Visibility = Visibility.Visible;
  75. pnlProgress.Visibility = Visibility.Collapsed;
  76. }
  77. }
  78. private void DownWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  79. {
  80. downWebClient.Dispose(); // 清理资源
  81. if (e.Error != null)
  82. {
  83. MessageBox.Show(e.Error.Message);
  84. pnlUpdate.Visibility = Visibility.Visible;
  85. pnlProgress.Visibility = Visibility.Collapsed;
  86. return;
  87. }
  88. try
  89. {
  90. string desDownFile = e.UserState.ToString();
  91. string zipPath = System.IO.Path.GetDirectoryName(desDownFile);
  92. //string zipPath = AppDomain.CurrentDomain.BaseDirectory;
  93. ICSharpCode.SharpZipLib.ZipHelper.UnZip(desDownFile, zipPath, "", true);
  94. string downPath = Path.GetDirectoryName(desDownFile);
  95. File.Delete(desDownFile);
  96. MessageBox.Show("升级完成,将自动重新启动程序。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  97. RestartApp(zipPath);
  98. }
  99. catch (Exception ex)
  100. {
  101. MessageBox.Show(ex.Message);
  102. }
  103. //move mydown_ty.exe c:\my.exe
  104. }
  105. private void DownWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  106. {
  107. // 使用 Dispatcher 来确保在主线程中更新 UI
  108. Dispatcher.Invoke(() =>
  109. {
  110. downProgress.Value = e.ProgressPercentage;
  111. txtProgress.Text = $"已下载 {e.ProgressPercentage}%";
  112. });
  113. }
  114. //移动下载好的文件并启动exe
  115. private static void RestartApp(string zipPath)
  116. {
  117. string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  118. StringBuilder sb = new StringBuilder();
  119. sb.Append(@" /C ping 1.1.1.1 -n 1 -w 1000 > Nul");
  120. sb.Append($" & MOVE {zipPath}\\* {baseDirectory}");
  121. sb.Append($" & rd /s /q {zipPath}");
  122. string sPf = Path.GetPathRoot(baseDirectory).Substring(0, 2);
  123. sb.Append($" & {sPf}");
  124. sb.Append($" & cd {baseDirectory}");
  125. sb.Append($" & {Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName)}");
  126. ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", sb.ToString())
  127. {
  128. WindowStyle = ProcessWindowStyle.Hidden,
  129. CreateNoWindow = false
  130. };
  131. Process.Start(psi);
  132. Application.Current.Shutdown();
  133. }
  134. /// <summary>
  135. /// 在命令行窗口中执行
  136. /// </summary>
  137. /// <param name="sExePath"></param>
  138. /// <param name="sArguments"></param>
  139. static void CmdStartCTIProc(string sExePath, string sArguments)
  140. {
  141. Process p = new Process();
  142. p.StartInfo.FileName = "cmd.exe";
  143. p.StartInfo.UseShellExecute = false;
  144. p.StartInfo.RedirectStandardInput = true;
  145. p.StartInfo.RedirectStandardOutput = true;
  146. p.StartInfo.RedirectStandardError = true;
  147. p.StartInfo.CreateNoWindow = false;
  148. //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  149. p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  150. //ProcessWindowStyle.Minimized;
  151. //| ProcessWindowStyle.Hidden;
  152. //System.Diagnostics.ProcessWindowStyle.Hidden;
  153. //p.Start();
  154. //p.StandardInput.WriteLine(sExePath + " " + sArguments);
  155. string sPf = Path.GetPathRoot(sExePath);
  156. sPf = sPf.Substring(0, 2);
  157. p.StandardInput.WriteLine(sPf);
  158. string sMl = Path.GetDirectoryName(sExePath);
  159. p.StandardInput.WriteLine("cd " + sMl);
  160. //p.StandardInput.WriteLine(Path.get)
  161. //p.StandardInput.WriteLine(sExePath);
  162. p.StandardInput.WriteLine(Path.GetFileName(sExePath));
  163. p.StandardInput.WriteLine("exit");
  164. p.Close();
  165. System.Threading.Thread.Sleep(5000);//必须等待,否则重启的程序还未启动完成;根据情况调整等待时间
  166. }
  167. /// <summary>
  168. /// 转换字节大小
  169. /// </summary>
  170. /// <param name="byteSize">输入字节数</param>
  171. /// <returns>返回值</returns>
  172. private static string ConvertSize(long byteSize)
  173. {
  174. string str = "";
  175. float tempf = (float)byteSize;
  176. if (tempf / 1024 > 1)
  177. {
  178. if ((tempf / 1024) / 1024 > 1)
  179. {
  180. str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB";
  181. }
  182. else
  183. {
  184. str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB";
  185. }
  186. }
  187. else
  188. {
  189. str = tempf.ToString(CultureInfo.InvariantCulture) + "B";
  190. }
  191. return str;
  192. }
  193. private void BtnClose_Click(object sender, RoutedEventArgs e)
  194. {
  195. this.Close();
  196. }
  197. //------------------------------------------------
  198. ////////////////////////////////////////////////////////////////
  199. }
  200. }