123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Microsoft.WindowsAPICodePack.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- namespace Phihong_EVSE_UI_Tool
- {
- /// <summary>
- /// SaveSdCard.xaml 的互動邏輯
- /// </summary>
- public partial class SaveSdCard : UserControl
- {
- private Properties.Settings mySettings = Properties.Settings.Default;
- private string outputFolderPath;
- private string settingModelName;
- private List<string> toBeSavedFiles = new List<string>();
- public SaveSdCard()
- {
- InitializeComponent();
- outputFolderPath = mySettings.OutputFolderPath;
- uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
- uxModelNameTestBox.Text = mySettings.ModelName;
- }
- private void SaveToDirectory()
- {
- if (Directory.Exists(Utility.ZIP_SOURCE_DIRECTORY))
- {
- Directory.Delete(Utility.ZIP_SOURCE_DIRECTORY, true);
- }
- if (Directory.Exists(Utility.ZIP_FILE_DIRECTORY))
- {
- Directory.Delete(Utility.ZIP_FILE_DIRECTORY, true);
- }
- Directory.CreateDirectory(Utility.ZIP_SOURCE_DIRECTORY);
- Directory.CreateDirectory(Utility.ZIP_FILE_DIRECTORY);
- if (uxSaveAllRadioButton.IsChecked.Value)
- {
- toBeSavedFiles = new List<string>(Utility.ChangedFiles.Keys);
- }
- else
- {
- toBeSavedFiles = (from p in Utility.ChangedFiles
- where p.Value
- select p.Key).ToList<string>();
- }
- foreach (string fileName in toBeSavedFiles)
- {
- File.Copy(Path.Combine(Utility.OUTPUT_DIRECTORY, fileName),
- Path.Combine(Utility.ZIP_SOURCE_DIRECTORY, fileName), true);
- }
- foreach (string resName in Utility.REQ_FILELIST)
- {
- Uri srcPath = new Uri(Path.Combine(Utility.REQ_PARENTFOLDER, resName), UriKind.Relative);
- Utility.CopyFileFromResource(srcPath, Path.Combine(Utility.ZIP_SOURCE_DIRECTORY, resName));
- }
- string modelName = uxModelNameTestBox.Text;
- string zipFileName = string.Format("DWIN_SET_{0}_{1}.zip", modelName, DateTime.Now.ToString("yyyyMMddHHmmss"));
- string temZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, Utility.TEMP_ZIP_FILE_NAME);
- ZipFile.CreateFromDirectory(Utility.ZIP_SOURCE_DIRECTORY, temZipPath, CompressionLevel.Fastest, true);
- string tempHeaderBuildedZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, zipFileName);
- FirmwareHeaderBuilder.AddFirmwareHeader(temZipPath, modelName, tempHeaderBuildedZipPath);
- File.Copy(tempHeaderBuildedZipPath, Path.Combine(outputFolderPath, zipFileName));
- switch (toBeSavedFiles.Count())
- {
- case 0:
- uxMessageTextBlock.Text = "Archived no image and 11 required files to File";
- break;
- case 1:
- uxMessageTextBlock.Text = "Archived 1 image and 11 required files to File";
- break;
- default:
- uxMessageTextBlock.Text = "Archived " + toBeSavedFiles.Count() + " images and 11 required files to File";
- break;
- }
- System.Diagnostics.Process process = new System.Diagnostics.Process();
- process.StartInfo.FileName = outputFolderPath;
- process.Start();
- }
- private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
- {
- Button btn = e.Source as Button;
- if (btn is null)
- {
- return;
- }
- switch (btn.Tag.ToString())
- {
- case "Browse":
- var dlg = new CommonOpenFileDialog()
- {
- IsFolderPicker = true,
- Title = "Locate the output directory",
- };
- if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
- {
- outputFolderPath = dlg.FileName;
- uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
- //mySettings.SdCardPath = sdFolderPath;
- mySettings.OutputFolderPath = outputFolderPath;
- mySettings.Save();
- }
- break;
- case "Save":
- if (uxModelNameTestBox.Text.Length != 14)
- {
- MessageBox.Show("Model Name length shoud be 14",
- "Invalid Model Name", MessageBoxButton.OK, MessageBoxImage.Error);
- break;
- }
- mySettings.ModelName = uxModelNameTestBox.Text;
- mySettings.Save();
- if (!Directory.Exists(outputFolderPath))
- {
- MessageBox.Show("Please locate the output directory first",
- "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
- break;
- }
- SaveToDirectory();
- break;
- default:
- break;
- }
- }
- }
- }
|