WndUpdateAsk.xaml.cs 8.1 KB

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