StartupRestoreForm.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace Optimizer {
  9. public sealed partial class StartupRestoreForm : Form {
  10. string[] _backups;
  11. public StartupRestoreForm() {
  12. InitializeComponent();
  13. CheckForIllegalCrossThreadCalls = false;
  14. OptionsHelper.ApplyTheme(this);
  15. // translate UI elements
  16. if (OptionsHelper.CurrentOptions.LanguageCode != LanguageCode.EN) Translate();
  17. RefreshBackups();
  18. }
  19. private void RefreshBackups() {
  20. _backups = Directory.GetFiles(CoreHelper.StartupItemsBackupFolder, "*.json");
  21. Array.Reverse(_backups);
  22. listRestoreItems.Items.Clear();
  23. txtNoBackups.Visible = _backups.Length == 0;
  24. foreach (string x in _backups) {
  25. listRestoreItems.Items.Add(Path.GetFileNameWithoutExtension(x));
  26. }
  27. if (_backups.Any()) listRestoreItems.SelectedIndex = 0;
  28. }
  29. private void Translate() {
  30. this.Text = OptionsHelper.TranslationList["StartupRestoreForm"];
  31. Dictionary<string, string> translationList = OptionsHelper.TranslationList.ToObject<Dictionary<string, string>>();
  32. Control element;
  33. foreach (var x in translationList) {
  34. if (x.Key == null || x.Key == string.Empty) continue;
  35. element = this.Controls.Find(x.Key, true).FirstOrDefault();
  36. if (element == null) continue;
  37. element.Text = x.Value;
  38. }
  39. }
  40. private void StartupRestoreForm_Load(object sender, EventArgs e) {
  41. }
  42. // DeleteStartupBackup
  43. private void button2_Click(object sender, EventArgs e) {
  44. if (listRestoreItems.SelectedIndex > -1) {
  45. if (MessageBox.Show("Do you really want to delete this backup?", "Delete Backup?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
  46. try {
  47. File.Delete(_backups[listRestoreItems.SelectedIndex]);
  48. }
  49. catch (Exception ex) {
  50. Logger.LogError("StartupRestoreForm.DeleteStartupBackup", ex.Message, ex.StackTrace);
  51. }
  52. RefreshBackups();
  53. }
  54. }
  55. }
  56. private void ShowPreview() {
  57. if (listRestoreItems.SelectedIndex > -1) {
  58. List<BackupStartupItem> backup = JsonConvert.DeserializeObject<List<BackupStartupItem>>(File.ReadAllText(_backups[listRestoreItems.SelectedIndex]));
  59. StartupPreviewForm f = new StartupPreviewForm(backup);
  60. f.ShowDialog(this);
  61. }
  62. }
  63. private void button39_Click(object sender, EventArgs e) {
  64. ShowPreview();
  65. }
  66. private void listRestoreItems_MouseDoubleClick(object sender, MouseEventArgs e) {
  67. ShowPreview();
  68. }
  69. // RestoreStartupBackup
  70. private void button1_Click(object sender, EventArgs e) {
  71. if (listRestoreItems.SelectedIndex > -1) {
  72. List<BackupStartupItem> backup = JsonConvert.DeserializeObject<List<BackupStartupItem>>(File.ReadAllText(_backups[listRestoreItems.SelectedIndex]));
  73. string keyPath = string.Empty;
  74. RegistryKey hive = null;
  75. foreach (BackupStartupItem x in backup) {
  76. if (x.RegistryLocation == StartupItemLocation.HKLM.ToString()) {
  77. hive = Registry.LocalMachine;
  78. if (x.StartupType == StartupItemType.Run.ToString()) {
  79. keyPath = StartupHelper.LocalMachineRun;
  80. }
  81. else if (x.StartupType == StartupItemType.RunOnce.ToString()) {
  82. keyPath = StartupHelper.LocalMachineRunOnce;
  83. }
  84. }
  85. else if (x.RegistryLocation == StartupItemLocation.HKLMWoW.ToString()) {
  86. hive = Registry.LocalMachine;
  87. if (x.StartupType == StartupItemType.Run.ToString()) {
  88. keyPath = StartupHelper.LocalMachineRunWoW;
  89. }
  90. else if (x.StartupType == StartupItemType.RunOnce.ToString()) {
  91. keyPath = StartupHelper.LocalMachineRunOnceWow;
  92. }
  93. }
  94. else if (x.RegistryLocation == StartupItemLocation.HKCU.ToString()) {
  95. hive = Registry.CurrentUser;
  96. if (x.StartupType == StartupItemType.Run.ToString()) {
  97. keyPath = StartupHelper.CurrentUserRun;
  98. }
  99. else if (x.StartupType == StartupItemType.RunOnce.ToString()) {
  100. keyPath = StartupHelper.CurrentUserRunOnce;
  101. }
  102. }
  103. if (hive != null) {
  104. try {
  105. RegistryKey key = hive.OpenSubKey(keyPath, true);
  106. key.SetValue(x.Name, x.FileLocation, RegistryValueKind.String);
  107. }
  108. catch (Exception ex) {
  109. Logger.LogError("StartupRestoreForm.RestoreStartupBackup", ex.Message, ex.StackTrace);
  110. }
  111. }
  112. }
  113. this.Close();
  114. }
  115. }
  116. }
  117. }