private void buttonRestore_Click(object sender, RoutedEventArgs e) { long lSeqNum = 0; if (ListViewFiles.SelectedItem == null) { MessageBox.Show(Application.Current.MainWindow, "No restore file selected", Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return; } if ( MessageBox.Show(Application.Current.MainWindow, "Are you sure?", Utils.ProductName, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } var filePath = (ListViewFiles.SelectedItem as RestoreFile)?.FileInfo.FullName; using (var backupReg = new BackupRegistry(filePath)) { string message; if (!backupReg.Open(true)) { message = $"Failed to open backup file ({filePath}).\nUnable to restore registry."; MessageBox.Show(Application.Current.MainWindow, message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return; } try { SysRestore.StartRestore("Before Little System Cleaner (Registry Cleaner) Restore", out lSeqNum); } catch (Win32Exception ex) { message = $"Unable to create system restore point.\nThe following error occurred: {ex.Message}"; MessageBox.Show(Application.Current.MainWindow, message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } if (!backupReg.Deserialize(out message)) { MessageBox.Show(Application.Current.MainWindow, message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return; } if (backupReg.RegistryEntries.Count == 0) { MessageBox.Show(Application.Current.MainWindow, "No registry entries found in backup file.\nUnable to restore registry.", Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return; } if (backupReg.Restore()) { MessageBox.Show(Application.Current.MainWindow, "Successfully restored registry", Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Information); if (Settings.Default.registryCleanerOptionsDelBackup) { // Delete file (ListViewFiles.SelectedItem as RestoreFile)?.FileInfo.Delete(); // Remove from listview and refresh RestoreFiles.Remove(ListViewFiles.SelectedItem as RestoreFile); PopulateListView(); } } else { MessageBox.Show(Application.Current.MainWindow, "Error restoring the registry", Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } if (lSeqNum == 0) { return; } try { SysRestore.EndRestore(lSeqNum); } catch (Win32Exception ex) { message = $"Unable to create system restore point.\nThe following error occurred: {ex.Message}"; MessageBox.Show(Application.Current.MainWindow, message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } } }
private void PopulateListView() { DirectoryInfo di; try { di = new DirectoryInfo(Settings.Default.OptionsBackupDir); // If directory doesnt exist -> create it if (!di.Exists) { di.Create(); } // If list is already populated -> clear it if (RestoreFiles.Count > 0) { RestoreFiles.Clear(); } } catch (Exception ex) { string message = $"Unable to get files from backup directory.\nThe following error occurred: {ex.Message}"; MessageBox.Show(Application.Current.MainWindow, message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return; } foreach ( var fi in di.GetFiles().Where(fi => string.Compare(fi.Extension, ".bakx", StringComparison.Ordinal) == 0)) { // Deserialize to creation date using (var backupReg = new BackupRegistry(fi.FullName)) { if (!backupReg.Open(true)) { continue; } string error; if (!backupReg.Deserialize(out error)) { continue; } _restoreFiles.Add(new RestoreFile(fi, backupReg.Created)); } } // Refresh listview ListViewFiles.Items.Refresh(); // Auto resize columns if (ListViewFiles.Items.Count > 0) { ListViewFiles.AutoResizeColumns(); } }
/// <summary> /// Fixes registry problems /// </summary> /// <returns>Returns true if fix was successful</returns> private bool FixProblems() { var cancelled = false; try { var badRegKeys = GetSelectedRegKeys(); long lSeqNum = 0; BackupRegistry backupReg; if (SysRestore.SysRestoreAvailable()) { // Create Restore Point ProgressBarText = "Creating system restore point"; try { SysRestore.StartRestore("Before Little System Cleaner (Registry Cleaner) Fix", out lSeqNum); } catch (Win32Exception ex) { string message = $"Unable to create system restore point.\nThe following error occurred: {ex.Message}"; Utils.MessageBoxThreadSafe(message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } } // Generate filename to backup registry ProgressBarText = "Creating backup file"; var backupFile = string.Format("{0}\\{1:yyyy}_{1:MM}_{1:dd}_{1:HH}{1:mm}{1:ss}.bakx", Settings.Default.OptionsBackupDir, DateTime.Now); try { backupReg = new BackupRegistry(backupFile); backupReg.Open(false); } catch (Exception ex) { string message = $"Unable to create backup file ({backupFile}).\nError: {ex.Message}"; Utils.MessageBoxThreadSafe(message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } Settings.Default.lastScanErrorsFixed = 0; foreach (var brk in badRegKeys) { var skip = false; if (_cancellationTokenSource.IsCancellationRequested) { break; } // Backup key if (!backupReg.Store(brk)) { if (Settings.Default.registryCleanerOptionsShowErrors) { string message = $"An error occurred trying to backup registry key ({brk.RegKeyPath}).\nWould you like to remove it (not recommended)?"; if ( Utils.MessageBoxThreadSafe(message, Utils.ProductName, MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes) { skip = true; } } else { skip = !Settings.Default.registryCleanerOptionsDeleteOnBackupError; } } if (!skip) { // Delete key/value if (!brk.Delete()) { if (Settings.Default.registryCleanerOptionsShowErrors) { string message = $"An error occurred trying to remove registry key {brk.RegKeyPath}"; Utils.MessageBoxThreadSafe(message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } } else { // Set last scan erors fixed Settings.Default.lastScanErrorsFixed++; } } Dispatcher.Invoke(() => { // Set icon to check mark brk.BitmapImg = new Image { Source = BitmapSrcFinishedScanning }; Tree.Items.Refresh(); // Increase & Update progress bar ProgressBarValue++; ProgressBarText = $"Items Repaired: {ProgressBarValue}/{ProgressBar.Maximum}"; }); } if (!_cancellationTokenSource.IsCancellationRequested) { // Store data as file backupReg.Serialize(); } // Set total errors fixed Settings.Default.totalErrorsFixed += Settings.Default.lastScanErrorsFixed; if (SysRestore.SysRestoreAvailable()) { // Finish creating restore point if (lSeqNum != 0) { try { if (!_cancellationTokenSource.IsCancellationRequested) { SysRestore.EndRestore(lSeqNum); } else { SysRestore.CancelRestore(lSeqNum); } } catch (Win32Exception ex) { string message = $"Unable to create system restore point.\nThe following error occurred: {ex.Message}"; Utils.MessageBoxThreadSafe(message, Utils.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); } } } _cancellationTokenSource.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { cancelled = true; } finally { _cancellationTokenSource.Dispose(); _cancellationTokenSource = null; } return(!cancelled); }