StartupRestoreForm.cs 6.1 KB

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