SaveSdCard.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. switch (toBeSavedFiles.Count())
  51. {
  52. case 0:
  53. uxMessageTextBlock.Text = "Saved no file";
  54. break;
  55. case 1:
  56. uxMessageTextBlock.Text = "Saved 1 file to SD card";
  57. break;
  58. default:
  59. uxMessageTextBlock.Text = "Saved " + toBeSavedFiles.Count() + " files to SD card";
  60. break;
  61. }
  62. System.Diagnostics.Process process = new System.Diagnostics.Process();
  63. process.StartInfo.FileName = Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR);
  64. process.Start();
  65. }
  66. private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
  67. {
  68. Button btn = e.Source as Button;
  69. if (btn is null)
  70. {
  71. return;
  72. }
  73. switch (btn.Tag.ToString())
  74. {
  75. case "Browse":
  76. var dlg = new CommonOpenFileDialog()
  77. {
  78. IsFolderPicker = true,
  79. Title = "Locate the SD card",
  80. };
  81. if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
  82. {
  83. sdFolderPath = dlg.FileName;
  84. uxSdBrowseTextBox.Text = sdFolderPath;
  85. mySettings.SdCardPath = sdFolderPath;
  86. mySettings.Save();
  87. }
  88. break;
  89. case "Save":
  90. if(!Directory.Exists(sdFolderPath))
  91. {
  92. MessageBox.Show("Please locate the SD card path first",
  93. "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
  94. break;
  95. }
  96. SaveToSdCard();
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }
  103. }