LD-Uninstaller/src-csharp/LDUninstaller.UI/Dialogs/BackupRestoreWindow.xaml.cs

185 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using LDUninstaller.Core.Junk;
using LDUninstaller.Core.Models;
namespace LDUninstaller.UI.Dialogs
{
public class BackupFileItem
{
public string Name { get; set; } = string.Empty;
public string FullPath { get; set; } = string.Empty;
public DateTime LastWriteTime { get; set; }
public long Size { get; set; }
public string SizeDisplay => FileSize.FromBytes(Size).ToString();
}
public partial class BackupRestoreWindow : Window
{
public BackupRestoreWindow()
{
InitializeComponent();
Loaded += BackupRestoreWindow_Loaded;
}
private void BackupRestoreWindow_Loaded(object sender, RoutedEventArgs e)
{
LoadBackups();
LoadExclusions();
}
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void LoadBackups()
{
string backupDir = JunkManager.BackupDirectory;
if (!Directory.Exists(backupDir))
{
dgBackups.ItemsSource = new List<BackupFileItem>();
return;
}
var di = new DirectoryInfo(backupDir);
var items = di.GetFiles("*.reg")
.Concat(di.GetFiles("*.txt"))
.OrderByDescending(f => f.LastWriteTime)
.Select(f => new BackupFileItem
{
Name = f.Name,
FullPath = f.FullName,
LastWriteTime = f.LastWriteTime,
Size = f.Length
})
.ToList();
dgBackups.ItemsSource = items;
}
private void LoadExclusions()
{
var items = ExclusionManager.GetAllExclusions();
dgExclusions.ItemsSource = items;
}
private void BtnRestore_Click(object sender, RoutedEventArgs e)
{
if (dgBackups.SelectedItem is not BackupFileItem item)
{
MessageBox.Show("Lütfen geri yüklemek istediğiniz bir .reg yedek dosyası seçin.", "Yedek Seçilmedi", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (!item.Name.EndsWith(".reg", StringComparison.OrdinalIgnoreCase))
{
MessageBox.Show("Yalnızca .reg biçimindeki kayıt defteri yedekleri doğrudan geri yüklenebilir.", "Uyarı", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var confirm = MessageBox.Show(
$"'{item.Name}' yedeğini Windows Kayıt Defteri'ne geri aktarmak istiyor musunuz?\n\nDosya: {item.FullPath}",
"Geri Yükleme Onayı",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (confirm != MessageBoxResult.Yes) return;
try
{
var psi = new ProcessStartInfo("reg.exe", $"import \"{item.FullPath}\"")
{
UseShellExecute = true,
Verb = "runas"
};
using var proc = Process.Start(psi);
proc?.WaitForExit(5000);
if (proc != null && proc.ExitCode == 0)
{
MessageBox.Show("Kayıt defteri yedeği başarıyla geri yüklendi!", "Başarılı", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show($"Geri yükleme işlemi tamamlanamadı (Çıkış kodu: {proc?.ExitCode}).", "Hata", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Geri yükleme sırasında hata oluştu: {ex.Message}", "Hata", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnOpenFolder_Click(object sender, RoutedEventArgs e)
{
string backupDir = JunkManager.BackupDirectory;
if (!Directory.Exists(backupDir))
Directory.CreateDirectory(backupDir);
Process.Start("explorer.exe", backupDir);
}
private void BtnDeleteBackup_Click(object sender, RoutedEventArgs e)
{
if (dgBackups.SelectedItem is not BackupFileItem item)
{
MessageBox.Show("Lütfen silmek istediğiniz bir yedek dosyası seçin.", "Yedek Seçilmedi", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var confirm = MessageBox.Show($"'{item.Name}' dosyasını silmek istediğinize emin misiniz?", "Yedek Silme Onayı", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (confirm != MessageBoxResult.Yes) return;
try
{
if (File.Exists(item.FullPath))
File.Delete(item.FullPath);
LoadBackups();
}
catch (Exception ex)
{
MessageBox.Show($"Dosya silinemedi: {ex.Message}", "Hata", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnRemoveExclusion_Click(object sender, RoutedEventArgs e)
{
if (dgExclusions.SelectedItem is not ExclusionItem item)
{
MessageBox.Show("Lütfen koruma listesinden kaldırmak istediğiniz bir kural seçin.", "Kural Seçilmedi", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
ExclusionManager.RemoveExclusion(item.Path);
LoadExclusions();
}
private void BtnAddExclusion_Click(object sender, RoutedEventArgs e)
{
var dialog = new Microsoft.Win32.OpenFolderDialog
{
Title = "Korumaya almak istediğiniz klasörü seçin:"
};
if (dialog.ShowDialog() == true && !string.IsNullOrWhiteSpace(dialog.FolderName))
{
ExclusionManager.AddExclusion(dialog.FolderName, "Kullanıcı Manuel Koruması");
LoadExclusions();
}
}
}
}