/// <summary> /// Removes any registers with the same name that does not match the given reg. /// useful when there is a chance that the app might be moved a lot. /// </summary> /// <param name="reg">The ideal registeration details.</param> public static void KillMisfittingRegisters(AppStartupRegisteration reg) { var n = GetRegisterations().Where(r => r.Name.Equals(reg.Name)); foreach (var miss in n.Where(r => !r.Equals(reg))) { miss.Unregister(); } }
/// <summary> /// returns all startup registerations. /// </summary> public static IEnumerable <AppStartupRegisteration> GetRegisterations() { //read startup folder foreach (var file in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Startup)).Where(f => f.EndsWith(".lnk"))) { AppStartupRegisteration reg = null; try { var target = getShortcutTarget(file); if (target == null) { continue; } reg = new AppStartupRegisteration(StartupType.DefaultStartupFolder, target, Path.GetFileNameWithoutExtension(file)); } catch {} if (reg != null) { yield return(reg); } } RegistryKey add; //read registry user try { add = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.Default, RegistryRights.ReadKey); } catch (SecurityException) { goto _next; } if (add == null) { goto _next; } foreach (var name in add.GetValueNames()) { yield return(new AppStartupRegisteration(StartupType.MachineStartupRegistry, add.GetValue(name).As <string>().Trim('\"'), name)); } //read registry machine _next: try { add = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.Default, RegistryRights.ReadKey); } catch (SecurityException) { yield break; } if (add == null) { goto _next; } foreach (var name in add.GetValueNames()) { yield return(new AppStartupRegisteration(StartupType.LocalUserStartupRegistry, add.GetValue(name).As <string>().Trim('\"'), name)); } }
protected bool Equals(AppStartupRegisteration other) { return(Type == other.Type && string.Equals(Path, other.Path) && string.Equals(Name, other.Name)); }