StartupHelper.cs 7.6 KB

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