StartupRestoreForm.cs 5.4 KB

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