private static void GetRegistryStartupItemsHelper(ref List <StartupItem> list, StartupItemLocation location, StartupItemType type) { string keyPath = string.Empty; RegistryKey hive = null; if (location == StartupItemLocation.HKLM) { hive = Registry.LocalMachine; if (type == StartupItemType.Run) { keyPath = LocalMachineRun; } else if (type == StartupItemType.RunOnce) { keyPath = LocalMachineRunOnce; } } else if (location == StartupItemLocation.HKLMWoW) { hive = Registry.LocalMachine; if (type == StartupItemType.Run) { keyPath = LocalMachineRunWoW; } else if (type == StartupItemType.RunOnce) { keyPath = LocalMachineRunOnceWow; } } else if (location == StartupItemLocation.HKCU) { hive = Registry.CurrentUser; if (type == StartupItemType.Run) { keyPath = CurrentUserRun; } else if (type == StartupItemType.RunOnce) { keyPath = CurrentUserRunOnce; } } if (hive != null) { RegistryKey key = hive.OpenSubKey(keyPath, true); if (key != null) { string[] valueNames = key.GetValueNames(); foreach (string x in valueNames) { try { RegistryStartupItem item = new RegistryStartupItem(); item.Name = x; item.FileLocation = key.GetValue(x).ToString(); item.Key = key; item.RegistryLocation = location; item.StartupType = type; list.Add(item); } catch (Exception ex) { ErrorLogger.LogError("Utilities.GetRegistryStartupItemsHelper", ex.Message, ex.StackTrace); } } } } }
internal static List <StartupItem> GetStartupItems() { List <StartupItem> collection = new List <StartupItem>(); RegistryKey registryKey = null; // Get Local Machine Run startup items try { registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRun, true); string[] valueNames = registryKey.GetValueNames(); foreach (string s in valueNames) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKLM; item.RegistryType = StartupItemType.Run; collection.Add(item); } } catch { } // Get Local Machine Run Once startup items try { registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnce, true); string[] valueNames = registryKey.GetValueNames(); foreach (string s in valueNames) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKLM; item.RegistryType = StartupItemType.RunOnce; collection.Add(item); } } catch { } // Get Local Machine Run WoW startup items try { registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunWoW, true); string[] valueNames2 = registryKey.GetValueNames(); foreach (string s in valueNames2) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKLMWoW; item.RegistryType = StartupItemType.Run; collection.Add(item); } } catch { } // Get Local Machine Run Once WoW startup items try { registryKey = Registry.LocalMachine.OpenSubKey(LocalMachineRunOnceWow, true); string[] valueNames2 = registryKey.GetValueNames(); foreach (string s in valueNames2) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKLMWoW; item.RegistryType = StartupItemType.RunOnce; collection.Add(item); } } catch { } // Get Current User Run startup items try { registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRun, true); string[] valueNames3 = registryKey.GetValueNames(); foreach (string s in valueNames3) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKCU; item.RegistryType = StartupItemType.Run; collection.Add(item); } } catch { } // Get Current User Run Once startup items try { registryKey = Registry.CurrentUser.OpenSubKey(CurrentUserRunOnce, true); string[] valueNames3 = registryKey.GetValueNames(); foreach (string s in valueNames3) { RegistryStartupItem item = new RegistryStartupItem(); item.Name = s; item.Location = registryKey.GetValue(s).ToString(); item.Key = registryKey; item.RegistryLocation = StartupItemLocation.HKCU; item.RegistryType = StartupItemType.RunOnce; collection.Add(item); } } catch { } // Get Current User Startup folder startup items try { // get shortcuts to files string[] shortcuts = Directory.GetFiles(CurrentUserStartupFolder, "*.lnk", SearchOption.AllDirectories); // get executables string[] files = Directory.GetFiles(CurrentUserStartupFolder, "*.exe", SearchOption.AllDirectories); foreach (string shortcut in shortcuts) { FolderStartupItem item = new FolderStartupItem(); item.Name = Path.GetFileNameWithoutExtension(shortcut); item.Location = GetShortcutTargetFile(shortcut); item.Shortcut = shortcut; item.RegistryLocation = StartupItemLocation.Folder; collection.Add(item); } foreach (string file in files) { FolderStartupItem item2 = new FolderStartupItem(); item2.Name = Path.GetFileNameWithoutExtension(file); item2.Location = file; item2.Shortcut = file; item2.RegistryLocation = StartupItemLocation.Folder; collection.Add(item2); } } catch { } // Get Local Machine Startup folder startup items try { // get shortcuts to files string[] shortcuts = Directory.GetFiles(LocalMachineStartupFolder, "*.lnk", SearchOption.AllDirectories); // get executables string[] files = Directory.GetFiles(LocalMachineStartupFolder, "*.exe", SearchOption.AllDirectories); foreach (string shortcut in shortcuts) { FolderStartupItem item = new FolderStartupItem(); item.Name = Path.GetFileNameWithoutExtension(shortcut); item.Location = GetShortcutTargetFile(shortcut); item.Shortcut = shortcut; item.RegistryLocation = StartupItemLocation.Folder; collection.Add(item); } foreach (string file in files) { FolderStartupItem item2 = new FolderStartupItem(); item2.Name = Path.GetFileNameWithoutExtension(file); item2.Location = file; item2.Shortcut = file; item2.RegistryLocation = StartupItemLocation.Folder; collection.Add(item2); } } catch { } return(collection); }