UpgradeView.xaml.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace AIPractice.LabellerServer.WPF
  6. {
  7. /// <summary>
  8. /// Interaction logic for UpgradeView.xaml
  9. /// </summary>
  10. public partial class UpgradeView : UserControl
  11. {
  12. public UpgradeView()
  13. {
  14. InitializeComponent();
  15. LoadUpgradeFile();
  16. }
  17. private void OnSelectFileClick(object sender, RoutedEventArgs e)
  18. {
  19. Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
  20. ofd.Filter = "*.exe|*.exe";
  21. if(ofd.ShowDialog() == true)
  22. {
  23. ClientFile.Text = ofd.FileName;
  24. }
  25. }
  26. private void OnImportClick(object sender, RoutedEventArgs e)
  27. {
  28. if(string.IsNullOrEmpty(ClientFile.Text) || !File.Exists(ClientFile.Text))
  29. {
  30. MessageBox.Show("请选择客户端安装文件。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  31. return;
  32. }
  33. if(string.IsNullOrEmpty(VersionInfo.Text) || !Version.TryParse(VersionInfo.Text, out _))
  34. {
  35. MessageBox.Show("请输入合法的版本号。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  36. return;
  37. }
  38. var version = VersionInfo.Text;
  39. var upgradeFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Upgrade");
  40. if (!Directory.Exists(upgradeFolder))
  41. {
  42. Directory.CreateDirectory(upgradeFolder);
  43. }
  44. var upgradeFile = Path.Combine(upgradeFolder, "Setup.exe");
  45. var upgradeVersionFile = Path.Combine(upgradeFolder, "Version.txt");
  46. File.Copy(ClientFile.Text, upgradeFile,true);
  47. File.WriteAllText(upgradeVersionFile,version);
  48. LabellerService.SetCurrentClient(version, File.ReadAllBytes(upgradeFile));
  49. UpgradeInfo.Content = $"当前更新包版本: {version}";
  50. LogManager.WriteLog($"更新包{ClientFile.Text}, 版本:{VersionInfo.Text} 已导入");
  51. }
  52. private void LoadUpgradeFile()
  53. {
  54. var upgradeFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Upgrade");
  55. if (Directory.Exists(upgradeFolder))
  56. {
  57. var upgradeFile = Path.Combine(upgradeFolder, "Setup.exe");
  58. var upgradeVersionFile = Path.Combine(upgradeFolder, "Version.txt");
  59. if (File.Exists(upgradeFile) && File.Exists(upgradeVersionFile))
  60. {
  61. var version = File.ReadAllText(upgradeVersionFile);
  62. LabellerService.SetCurrentClient(version, File.ReadAllBytes(upgradeFile));
  63. UpgradeInfo.Content = $"当前更新包版本: {version}";
  64. }
  65. }
  66. }
  67. }
  68. }