StartupHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace Optimizer {
  7. internal static class StartupHelper {
  8. internal static readonly string LocalMachineRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  9. internal static readonly string LocalMachineRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  10. internal static readonly string LocalMachineRunWoW = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";
  11. internal static readonly string LocalMachineRunOnceWow = "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  12. internal static readonly string CurrentUserRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  13. internal static readonly string CurrentUserRunOnce = "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
  14. internal static readonly string LocalMachineStartupFolder = CleanHelper.ProgramData + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  15. internal static readonly string CurrentUserStartupFolder = CleanHelper.ProfileAppDataRoaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
  16. private static void GetRegistryStartupItemsHelper(ref List<StartupItem> list, StartupItemLocation location, StartupItemType type) {
  17. string keyPath = string.Empty;
  18. RegistryKey hive = null;
  19. if (location == StartupItemLocation.HKLM) {
  20. hive = Registry.LocalMachine;
  21. if (type == StartupItemType.Run) {
  22. keyPath = LocalMachineRun;
  23. }
  24. else if (type == StartupItemType.RunOnce) {
  25. keyPath = LocalMachineRunOnce;
  26. }
  27. }
  28. else if (location == StartupItemLocation.HKLMWoW) {
  29. hive = Registry.LocalMachine;
  30. if (type == StartupItemType.Run) {
  31. keyPath = LocalMachineRunWoW;
  32. }
  33. else if (type == StartupItemType.RunOnce) {
  34. keyPath = LocalMachineRunOnceWow;
  35. }
  36. }
  37. else if (location == StartupItemLocation.HKCU) {
  38. hive = Registry.CurrentUser;
  39. if (type == StartupItemType.Run) {
  40. keyPath = CurrentUserRun;
  41. }
  42. else if (type == StartupItemType.RunOnce) {
  43. keyPath = CurrentUserRunOnce;
  44. }
  45. }
  46. if (hive != null) {
  47. try {
  48. RegistryKey key = hive.OpenSubKey(keyPath, true);
  49. if (key != null) {
  50. string[] valueNames = key.GetValueNames();
  51. foreach (string x in valueNames) {
  52. try {
  53. RegistryStartupItem item = new RegistryStartupItem();
  54. item.Name = x;
  55. item.FileLocation = key.GetValue(x).ToString();
  56. item.Key = key;
  57. item.RegistryLocation = location;
  58. item.StartupType = type;
  59. list.Add(item);
  60. }
  61. catch (Exception ex) {
  62. Logger.LogError("Utilities.GetRegistryStartupItemsHelper", ex.Message, ex.StackTrace);
  63. }
  64. }
  65. }
  66. }
  67. catch (Exception ex) {
  68. Logger.LogError("Utilities.GetRegistryStartupItemsHelper", ex.Message, ex.StackTrace);
  69. }
  70. }
  71. }
  72. private static void GetFolderStartupItemsHelper(ref List<StartupItem> list, IEnumerable<string> files, IEnumerable<string> shortcuts, StartupItemLocation folderOrigin) {
  73. foreach (string file in files) {
  74. try {
  75. FolderStartupItem item = new FolderStartupItem();
  76. item.Name = Path.GetFileNameWithoutExtension(file);
  77. item.FileLocation = file;
  78. item.Shortcut = file;
  79. item.RegistryLocation = folderOrigin;
  80. list.Add(item);
  81. }
  82. catch (Exception ex) {
  83. Logger.LogError("Utilities.GetFolderStartupItemsHelper", ex.Message, ex.StackTrace);
  84. }
  85. }
  86. foreach (string shortcut in shortcuts) {
  87. try {
  88. FolderStartupItem item = new FolderStartupItem();
  89. item.Name = Path.GetFileNameWithoutExtension(shortcut);
  90. item.FileLocation = Utilities.GetShortcutTargetFile(shortcut);
  91. item.Shortcut = shortcut;
  92. item.RegistryLocation = folderOrigin;
  93. list.Add(item);
  94. }
  95. catch (Exception ex) {
  96. Logger.LogError("Utilities.GetFolderStartupItemsHelper", ex.Message, ex.StackTrace);
  97. }
  98. }
  99. }
  100. internal static List<StartupItem> GetStartupItems() {
  101. List<StartupItem> startupItems = new List<StartupItem>();
  102. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.Run);
  103. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLM, StartupItemType.RunOnce);
  104. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.Run);
  105. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKCU, StartupItemType.RunOnce);
  106. if (Environment.Is64BitOperatingSystem) {
  107. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.Run);
  108. GetRegistryStartupItemsHelper(ref startupItems, StartupItemLocation.HKLMWoW, StartupItemType.RunOnce);
  109. }
  110. if (Directory.Exists(CurrentUserStartupFolder)) {
  111. IEnumerable<string> currentUserFiles = Directory.EnumerateFiles(CurrentUserStartupFolder, "*.*", SearchOption.AllDirectories)
  112. .Where(s => s.EndsWith(".exe") || s.EndsWith(".bat") || s.EndsWith(".cmd"));
  113. IEnumerable<string> currentUserShortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories);
  114. GetFolderStartupItemsHelper(ref startupItems, currentUserFiles, currentUserShortcuts, StartupItemLocation.CUStartupFolder);
  115. }
  116. if (Directory.Exists(LocalMachineStartupFolder)) {
  117. IEnumerable<string> localMachineFiles = Directory.EnumerateFiles(LocalMachineStartupFolder, "*.*", SearchOption.AllDirectories)
  118. .Where(s => s.EndsWith(".exe") || s.EndsWith(".bat") || s.EndsWith(".cmd"));
  119. IEnumerable<string> localMachineShortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories);
  120. GetFolderStartupItemsHelper(ref startupItems, localMachineFiles, localMachineShortcuts, StartupItemLocation.LMStartupFolder);
  121. }
  122. return startupItems;
  123. }
  124. }
  125. }