StartupRestoreForm.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. {
  10. public partial class StartupRestoreForm : Form
  11. {
  12. string[] _backups;
  13. public StartupRestoreForm()
  14. {
  15. InitializeComponent();
  16. CheckForIllegalCrossThreadCalls = false;
  17. Options.ApplyTheme(this);
  18. // translate UI elements
  19. if (Options.CurrentOptions.LanguageCode != LanguageCode.EN) Translate();
  20. RefreshBackups();
  21. }
  22. private void RefreshBackups()
  23. {
  24. _backups = Directory.GetFiles(Required.StartupItemsBackupFolder, "*.json");
  25. Array.Reverse(_backups);
  26. listRestoreItems.Items.Clear();
  27. txtNoBackups.Visible = _backups.Length == 0;
  28. foreach (string x in _backups)
  29. {
  30. listRestoreItems.Items.Add(Path.GetFileNameWithoutExtension(x));
  31. }
  32. }
  33. private void Translate()
  34. {
  35. this.Text = Options.TranslationList["StartupRestoreForm"];
  36. Dictionary<string, string> translationList = Options.TranslationList.ToObject<Dictionary<string, string>>();
  37. Control element;
  38. foreach (var x in translationList)
  39. {
  40. if (x.Key == null || x.Key == string.Empty) continue;
  41. element = this.Controls.Find(x.Key, true).FirstOrDefault();
  42. if (element == null) continue;
  43. element.Text = x.Value;
  44. }
  45. }
  46. private void StartupRestoreForm_Load(object sender, EventArgs e)
  47. {
  48. }
  49. // DeleteStartupBackup
  50. private void button2_Click(object sender, EventArgs e)
  51. {
  52. if (listRestoreItems.SelectedIndex > -1)
  53. {
  54. if (MessageBox.Show("Do you really want to delete this backup?", "Delete Backup?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  55. {
  56. try
  57. {
  58. File.Delete(_backups[listRestoreItems.SelectedIndex]);
  59. }
  60. catch (Exception ex)
  61. {
  62. ErrorLogger.LogError("StartupRestoreForm.DeleteStartupBackup", ex.Message, ex.StackTrace);
  63. }
  64. RefreshBackups();
  65. }
  66. }
  67. }
  68. private void ShowPreview()
  69. {
  70. if (listRestoreItems.SelectedIndex > -1)
  71. {
  72. List<StartupBackupItem> backup = JsonConvert.DeserializeObject<List<StartupBackupItem>>(File.ReadAllText(_backups[listRestoreItems.SelectedIndex]));
  73. StartupPreviewForm f = new StartupPreviewForm(backup);
  74. f.ShowDialog(this);
  75. }
  76. }
  77. private void button39_Click(object sender, EventArgs e)
  78. {
  79. ShowPreview();
  80. }
  81. private void listRestoreItems_MouseDoubleClick(object sender, MouseEventArgs e)
  82. {
  83. ShowPreview();
  84. }
  85. // RestoreStartupBackup
  86. private void button1_Click(object sender, EventArgs e)
  87. {
  88. if (listRestoreItems.SelectedIndex > -1)
  89. {
  90. List<StartupBackupItem> backup = JsonConvert.DeserializeObject<List<StartupBackupItem>>(File.ReadAllText(_backups[listRestoreItems.SelectedIndex]));
  91. string keyPath = string.Empty;
  92. RegistryKey hive = null;
  93. foreach (StartupBackupItem x in backup)
  94. {
  95. if (x.RegistryLocation == StartupItemLocation.HKLM.ToString())
  96. {
  97. hive = Registry.LocalMachine;
  98. if (x.StartupType == StartupItemType.Run.ToString())
  99. {
  100. keyPath = Utilities.LocalMachineRun;
  101. }
  102. else if (x.StartupType == StartupItemType.RunOnce.ToString())
  103. {
  104. keyPath = Utilities.LocalMachineRunOnce;
  105. }
  106. }
  107. else if (x.RegistryLocation == StartupItemLocation.HKLMWoW.ToString())
  108. {
  109. hive = Registry.LocalMachine;
  110. if (x.StartupType == StartupItemType.Run.ToString())
  111. {
  112. keyPath = Utilities.LocalMachineRunWoW;
  113. }
  114. else if (x.StartupType == StartupItemType.RunOnce.ToString())
  115. {
  116. keyPath = Utilities.LocalMachineRunOnceWow;
  117. }
  118. }
  119. else if (x.RegistryLocation == StartupItemLocation.HKCU.ToString())
  120. {
  121. hive = Registry.CurrentUser;
  122. if (x.StartupType == StartupItemType.Run.ToString())
  123. {
  124. keyPath = Utilities.CurrentUserRun;
  125. }
  126. else if (x.StartupType == StartupItemType.RunOnce.ToString())
  127. {
  128. keyPath = Utilities.CurrentUserRunOnce;
  129. }
  130. }
  131. if (hive != null)
  132. {
  133. try
  134. {
  135. RegistryKey key = hive.OpenSubKey(keyPath, true);
  136. key.SetValue(x.Name, x.FileLocation, RegistryValueKind.String);
  137. }
  138. catch (Exception ex)
  139. {
  140. ErrorLogger.LogError("StartupRestoreForm.RestoreStartupBackup", ex.Message, ex.StackTrace);
  141. }
  142. }
  143. }
  144. this.Close();
  145. }
  146. }
  147. }
  148. }