using Microsoft.Win32; using System; using System.IO; using System.Text; using System.Threading.Tasks; using System.Windows; using GetHrzLib.Models; namespace GetHrzWPF { public partial class MainWindow : Window { private string _currentFilePath; public MainWindow() { InitializeComponent(); } private void OnOpenClickimg(object sender, RoutedEventArgs e) { var dlg = new OpenFileDialog { Title = "Выбери файл .img", Filter = "IMG files (*.img)|*.img|All files (*.*)|*.*", Multiselect = false, CheckFileExists = true }; if (dlg.ShowDialog(this) == true) { // InfoLabel.Content = dlg.FileName; // LogBox.AppendText($"Открыт образ: {dlg.FileName}{Environment.NewLine}"); // LogBox.ScrollToEnd(); } string filePath = dlg.FileName; try { using var writer = new StringWriter(); BlockProcessor.Process(writer, filePath); _currentFilePath = filePath; InfoLabel.Content = filePath; LogBox.Text = writer.ToString(); LogBox.ScrollToEnd(); } catch (Exception ex) { LogBox.Text = $"Ошибка: {ex.Message}"; } } private async void OnOpenClickdisk(object sender, RoutedEventArgs e) { // 1) Берём список USB через вашу библиотеку var usbDrives = UsbDriveService.GetUsbDrivesSafe(); if (usbDrives.Count == 0) { MessageBox.Show(this, "Съёмные USB-диски не найдены.", "USB", MessageBoxButton.OK, MessageBoxImage.Information); return; } // 2) Окно выбора диска (как вы уже делали) var dlg = new SelectUsbWindow(usbDrives) { Owner = this }; if (dlg.ShowDialog() != true || dlg.SelectedDrive == null) return; // 3) Конвертируем "E:\" -> "\\.\E:" (RAW путь) string devicePath = dlg.SelectedDrive.ToFullPath(); try { ProgressBar.Visibility = Visibility.Visible; string result = await Task.Run(() => { using (var writer = new StringWriter()) { BlockProcessor.Process(writer, devicePath); return writer.ToString(); } }); _currentFilePath = devicePath; InfoLabel.Content = "USB (raw): " + devicePath; LogBox.Text = result; LogBox.ScrollToEnd(); } catch (UnauthorizedAccessException) { LogBox.Text = "Ошибка: отказано в доступе к USB как RAW-устройству.\r\n" + "Запустите приложение от имени администратора.\r\n" + $"Путь: {devicePath}"; } catch (Exception ex) { LogBox.Text = $"Ошибка: {ex.Message}"; } finally { ProgressBar.Visibility = Visibility.Collapsed; } } private async void GetHrzButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(_currentFilePath)) { MessageBox.Show(this, "Сначала выберите файл/диск для обработки.", "GetHrz", MessageBoxButton.OK, MessageBoxImage.Information); return; } // Диалог сохранения var sfd = new SaveFileDialog { Title = "Сохранить результат", FileName = "Restore_hrz.hrz", Filter = "HRZ files (*.hrz)|*.hrz|All files (*.*)|*.*", AddExtension = true, DefaultExt = ".hrz", OverwritePrompt = true }; if (sfd.ShowDialog(this) != true) { MessageBox.Show(this, "Сохранение отменено.", "GetHrz", MessageBoxButton.OK, MessageBoxImage.Information); return; } string savePath = sfd.FileName; ProgressBar.Visibility = Visibility.Visible; try { // Лог будем собирать в фоне var sb = new StringBuilder(); await Task.Run(() => { using (var writer = new StringWriter(sb)) { BlockProcessor.ProcessGetHrz(writer, _currentFilePath, savePath); } }); // Мы уже вернулись в UI поток после await LogBox.AppendText(sb.ToString()); LogBox.AppendText(Environment.NewLine); LogBox.ScrollToEnd(); } catch (UnauthorizedAccessException ex) { MessageBox.Show(this, "Ошибка доступа.\n" + ex.Message, "GetHrz", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(this, "Ошибка при повторном чтении:\n" + ex.Message, "GetHrz", MessageBoxButton.OK, MessageBoxImage.Error); } finally { ProgressBar.Visibility = Visibility.Collapsed; } } } }