SaveSdCard.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.WindowsAPICodePack.Dialogs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. namespace Phihong_EVSE_UI_Tool
  17. {
  18. /// <summary>
  19. /// SaveSdCard.xaml 的互動邏輯
  20. /// </summary>
  21. public partial class SaveSdCard : UserControl
  22. {
  23. private Properties.Settings mySettings = Properties.Settings.Default;
  24. private string sdFolderPath;
  25. private List<string> toBeSavedFiles = new List<string>();
  26. public SaveSdCard()
  27. {
  28. InitializeComponent();
  29. sdFolderPath = mySettings.SdCardPath;
  30. uxSdBrowseTextBox.Text = sdFolderPath;
  31. }
  32. private void SaveToSdCard()
  33. {
  34. if (uxSaveAllRadioButton.IsChecked.Value)
  35. {
  36. toBeSavedFiles = new List<string>(Utility.ChangedFiles.Keys);
  37. }
  38. else
  39. {
  40. toBeSavedFiles = (from p in Utility.ChangedFiles
  41. where p.Value
  42. select p.Key).ToList<string>();
  43. }
  44. Directory.CreateDirectory(Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR));
  45. foreach (string fileName in toBeSavedFiles)
  46. {
  47. File.Copy(Path.Combine(Utility.OUTPUT_DIRECTORY, fileName),
  48. Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR, fileName), true);
  49. }
  50. foreach (string resName in Utility.REQ_FILELIST)
  51. {
  52. Uri srcPath = new Uri(Path.Combine(Utility.REQ_PARENTFOLDER, resName), UriKind.Relative);
  53. Utility.CopyFileFromResource(srcPath, Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR, resName));
  54. }
  55. switch (toBeSavedFiles.Count())
  56. {
  57. case 0:
  58. uxMessageTextBlock.Text = "Saved no image and 11 required files to SD card";
  59. break;
  60. case 1:
  61. uxMessageTextBlock.Text = "Saved 1 image and 11 required files to SD card";
  62. break;
  63. default:
  64. uxMessageTextBlock.Text = "Saved " + toBeSavedFiles.Count() + " images and 11 required files to SD card";
  65. break;
  66. }
  67. System.Diagnostics.Process process = new System.Diagnostics.Process();
  68. process.StartInfo.FileName = Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR);
  69. process.Start();
  70. }
  71. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  72. {
  73. Button btn = e.Source as Button;
  74. if (btn is null)
  75. {
  76. return;
  77. }
  78. switch (btn.Tag.ToString())
  79. {
  80. case "Browse":
  81. var dlg = new CommonOpenFileDialog()
  82. {
  83. IsFolderPicker = true,
  84. Title = "Locate the SD card",
  85. };
  86. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  87. {
  88. sdFolderPath = dlg.FileName;
  89. uxSdBrowseTextBox.Text = sdFolderPath;
  90. mySettings.SdCardPath = sdFolderPath;
  91. mySettings.Save();
  92. }
  93. break;
  94. case "Save":
  95. if(!Directory.Exists(sdFolderPath))
  96. {
  97. MessageBox.Show("Please locate the SD card path first",
  98. "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
  99. break;
  100. }
  101. SaveToSdCard();
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. }
  108. }