StartupRestoreForm.cs 6.0 KB

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