private static string GetStartupApprovedKey(StartupEntry startupEntry)
        {
            if (!startupEntry.IsRegKey)
            {
                return(startupEntry.AllUsers
                    ? @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\StartupFolder"
                    : @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\StartupFolder");
            }

            if (startupEntry.IsRunOnce)
            {
                if (startupEntry.AllUsers)
                {
                    return(startupEntry.ParentLongName.Contains("Wow6432Node")
                        ? @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\RunOnce32"
                        : @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\RunOnce");
                }
                return(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\RunOnce");
            }

            if (startupEntry.AllUsers)
            {
                return(startupEntry.ParentLongName.Contains("Wow6432Node")
                    ? @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run32"
                    : @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run");
            }
            return(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run");
        }
 private static bool GetDisabled(StartupEntry startupEntry)
 {
     using (var key = RegistryTools.CreateSubKeyRecursively(GetStartupApprovedKey(startupEntry)))
     {
         var bytes = key.GetValue(startupEntry.EntryLongName) as byte[];
         return(bytes != null && bytes.Length > 0 && !bytes[0].Equals(0x02));
     }
 }
 private static void SetDisabled(StartupEntry startupEntry, bool disabled)
 {
     using (var key = RegistryTools.CreateSubKeyRecursively(GetStartupApprovedKey(startupEntry)))
     {
         key.SetValue(startupEntry.EntryLongName, disabled ? DisabledBytes : EnabledBytes,
                      RegistryValueKind.Binary);
     }
 }
        /// <summary>
        ///     Disable startup entry to stop it from being processed at startup. It is stored in the backup store.
        /// </summary>
        /// <param name="startupEntry"></param>
        public static void Disable(StartupEntry startupEntry)
        {
            if (startupEntry.DisabledStore)
            {
                return;
            }

            DisableFunctions.Disable(startupEntry);
        }
        public bool StillExists(StartupEntry startupEntry)
        {
            if (startupEntry.IsRegKey)
            {
                using (var key = RegistryTools.OpenRegistryKey(startupEntry.ParentLongName))
                    return(!string.IsNullOrEmpty(key.GetStringSafe(startupEntry.EntryLongName)));
            }

            return(File.Exists(startupEntry.FullLongName));
        }
Exemplo n.º 6
0
        public void Delete(StartupEntry startupEntry)
        {
            RemoveDisabledRegEntry(startupEntry);

            // Remove the backup file
            if (!startupEntry.IsRegKey)
            {
                File.Delete(GetDisabledEntryPath(startupEntry));
            }
        }
        /// <summary>
        ///     Set if this startup entry should run for all users or only for the current user.
        /// </summary>
        public static void SetAllUsers(StartupEntry startupEntry, bool allUsers)
        {
            // Find the suitable replacement
            var target = StartupEntryFactory.RunLocations.First(x => (x.IsRegKey == startupEntry.IsRegKey) &&
                                                                (x.IsRunOnce == startupEntry.IsRunOnce) &&
                                                                (x.AllUsers == allUsers) && !x.IsWow);

            // Don't want to deal with the disable wizardry
            var wasDisabled = startupEntry.Disabled;

            if (wasDisabled)
            {
                Enable(startupEntry);
            }

            // Remove old entry or move the link to the new directory.
            if (startupEntry.IsRegKey)
            {
                try
                {
                    // Can't do this with links as they would get deleted
                    startupEntry.Delete();
                }
                catch
                {
                    // Key doesn't exist
                }
            }
            else
            {
                if (File.Exists(startupEntry.FullLongName))
                {
                    var newPath = Path.Combine(target.Path, startupEntry.EntryLongName);
                    File.Delete(newPath);
                    File.Move(startupEntry.FullLongName, newPath);
                }
            }

            // Plug in new data
            startupEntry.SetParentLongName(target.Path);
            startupEntry.AllUsersStore = allUsers;

            // Update registry stuff
            if (startupEntry.IsRegKey)
            {
                CreateRegValue(startupEntry);
            }

            // Restore disable status
            if (wasDisabled)
            {
                Disable(startupEntry);
            }
        }
        /// <summary>
        ///     Create a registry value for the specified entry. Works for drive links as well.
        /// </summary>
        /// <param name="startupEntry"></param>
        internal static void CreateRegValue(StartupEntry startupEntry)
        {
            if (string.IsNullOrEmpty(startupEntry.Command))
            {
                return;
            }

            using (var runKey = RegistryTools.CreateSubKeyRecursively(startupEntry.ParentLongName))
            {
                runKey.SetValue(startupEntry.EntryLongName, startupEntry.Command, RegistryValueKind.String);
            }
        }
        /// <summary>
        ///     Delete startup entry data from registry and file system.
        ///     Only needed items are removed, for example if entry is disabled the entry from "Run" key is
        ///     not removed if it exists, same for the "Startup" folder. To remove them change the Disabled
        ///     property and run this command again.
        /// </summary>
        /// <param name="startupEntry">Entry to delete</param>
        public static void Delete(StartupEntry startupEntry)
        {
            if (startupEntry.Disabled)
            {
                DisableFunctions.Enable(startupEntry);
            }

            if (startupEntry.IsRegKey)
            {
                RegistryTools.RemoveRegistryValue(startupEntry.ParentLongName, startupEntry.EntryLongName);
            }
            else
            {
                File.Delete(startupEntry.FullLongName);
            }
        }
Exemplo n.º 10
0
 private static bool GetDisabled(StartupEntry startupEntry)
 {
     try
     {
         using (var key = RegistryTools.CreateSubKeyRecursively(GetStartupApprovedKey(startupEntry)))
         {
             var bytes = key.GetValue(startupEntry.EntryLongName) as byte[];
             return(bytes != null && bytes.Length > 0 && !bytes[0].Equals(0x02));
         }
     }
     catch (SystemException ex)
     {
         Console.WriteLine(ex);
         return(false);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        ///     Remove registry key of a disabled startup entry. Link file is not touched if it exists.
        /// </summary>
        private static void RemoveDisabledRegEntry(StartupEntry startupEntry)
        {
            using (var disabledStartupEntryStore = RegistryTools.OpenRegistryKey(
                       startupEntry.IsRegKey ? RegistryDisabledKey.Path : DriveDisabledKey.Path, true))
            {
                var disabledSubKeyName = startupEntry.IsRegKey
                    ? startupEntry.EntryLongName
                    : startupEntry.FullLongName.Replace('\\', '^');
                var disabledSubkeyKey =
                    disabledStartupEntryStore.GetSubKeyNames()
                    .FirstOrDefault(x => disabledSubKeyName.Equals(x, StringComparison.InvariantCultureIgnoreCase));

                if (!string.IsNullOrEmpty(disabledSubkeyKey))
                {
                    disabledStartupEntryStore.DeleteSubKey(disabledSubkeyKey);
                }
            }
        }
Exemplo n.º 12
0
        public void Disable(StartupEntry startupEntry)
        {
            if (startupEntry.DisabledStore)
            {
                return;
            }

            var newPath = CreateDisabledEntryPath(startupEntry);

            // Remove the startup entry / move the file to the backup folder
            if (startupEntry.IsRegKey)
            {
                try
                {
                    startupEntry.Delete();
                }
                catch
                {
                    // Key doesn't exist
                }
            }
            else
            {
                if (File.Exists(startupEntry.FullLongName))
                {
                    File.Delete(newPath);
                    if (!Directory.Exists(DriveDisableBackupPath))
                    {
                        Directory.CreateDirectory(DriveDisableBackupPath);
                    }
                    File.Move(startupEntry.FullLongName, newPath);
                    startupEntry.BackupPath = newPath;
                }
                else
                {
                    throw new InvalidOperationException(Localisation.StartupManager_FailedEnable_FileNotFound
                                                        + "\n\n" + startupEntry.FullLongName);
                }
            }

            CreateDisabledEntry(startupEntry, newPath);

            startupEntry.DisabledStore = true;
        }
Exemplo n.º 13
0
 public bool StillExists(StartupEntry startupEntry)
 {
     try
     {
         using (var key = RegistryTools.OpenRegistryKey(
                    startupEntry.IsRegKey ? RegistryDisabledKey.Path : DriveDisabledKey.Path))
         {
             var disabledSubKeyName = startupEntry.IsRegKey
                 ? startupEntry.EntryLongName
                 : startupEntry.FullLongName.Replace('\\', '^');
             return(key.GetSubKeyNames()
                    .Any(x => disabledSubKeyName.Equals(x, StringComparison.InvariantCultureIgnoreCase)));
         }
     }
     catch
     {
         return(false);
     }
 }
        /// <summary>
        ///     Crate backup of the entry in the specified directory. If backup file already exists, it is overwritten.
        /// </summary>
        public static void CreateBackup(StartupEntry startupEntry, string backupPath)
        {
            var newPath = Path.Combine(backupPath, "Startup - " + startupEntry.EntryLongName);

            if (startupEntry.IsRegKey)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Windows Registry Editor Version 5.00");
                sb.AppendLine();
                sb.AppendLine($@"[{startupEntry.ParentLongName}]");
                sb.AppendLine(
                    $"\"{startupEntry.EntryLongName}\"=\"{startupEntry.Command.Replace("\\", "\\\\").Replace("\"", "\\\"")}\"");
                File.WriteAllText(newPath + ".reg", sb.ToString());
            }
            else
            {
                if (!File.Exists(newPath))
                {
                    File.Delete(newPath);
                }

                if (startupEntry.Disabled)
                {
                    var disabledFile = DisableFunctions.GetDisabledEntryPath(startupEntry);
                    if (File.Exists(disabledFile))
                    {
                        File.Copy(disabledFile, newPath);
                    }
                }
                else
                {
                    if (File.Exists(startupEntry.FullLongName))
                    {
                        File.Copy(startupEntry.FullLongName, newPath);
                    }
                }
            }
        }
        /// <summary>
        ///     Look for links in startup folders
        /// </summary>
        private static IEnumerable <StartupEntry> GetDriveStartupItems(StartupPointData point)
        {
            if (!Directory.Exists(point.Path))
            {
                yield break;
            }

            foreach (var name in Directory.GetFiles(point.Path)
                     .Where(name => ".lnk".Equals(Path.GetExtension(name), StringComparison.CurrentCultureIgnoreCase)))
            {
                StartupEntry result;
                try
                {
                    result = new StartupEntry(point, Path.GetFileName(name), WindowsTools.ResolveShortcut(name));
                }
                catch (Exception ex)
                {
                    PremadeDialogs.GenericError(ex);
                    continue;
                }
                yield return(result);
            }
        }
        public static void MoveToRegistry(StartupEntry startupEntry)
        {
            if (startupEntry.IsRegKey)
            {
                return;
            }

            // Don't want to deal with the disable wizardry
            var wasDisabled = startupEntry.Disabled;

            if (wasDisabled)
            {
                Enable(startupEntry);
            }

            // Delete old entry
            startupEntry.Delete();

            // Plug in new data
            startupEntry.IsRegKey = true;

            var newPoint =
                StartupEntryFactory.RunLocations.First(x => x.IsRegKey && (x.AllUsers == startupEntry.AllUsers) &&
                                                       !x.IsRunOnce && !x.IsWow);

            startupEntry.SetParentLongName(newPoint.Path);
            startupEntry.SetParentFancyName(newPoint.Name);

            // Recreate registry entry
            CreateRegValue(startupEntry);

            // Restore disable status
            if (wasDisabled)
            {
                Disable(startupEntry);
            }
        }
Exemplo n.º 17
0
        public void Enable(StartupEntry startupEntry)
        {
            if (!startupEntry.DisabledStore)
            {
                return;
            }

            // Reconstruct the startup entry
            if (!startupEntry.IsRegKey)
            {
                // Move the backup file back to its original location
                var oldPath = GetDisabledEntryPath(startupEntry);

                if (File.Exists(oldPath))
                {
                    File.Delete(startupEntry.FullLongName);
                    File.Move(oldPath, startupEntry.FullLongName);
                }
                else
                {
                    throw new InvalidOperationException(Localisation.StartupManager_FailedEnable_FileNotFound
                                                        + "\n\n" + oldPath);
                }
            }
            else
            {
                // Recreate the registry key
                StartupEntryManager.CreateRegValue(startupEntry);
            }

            Delete(startupEntry);

            startupEntry.BackupPath = string.Empty;
            // Entry is no longer disabled
            startupEntry.DisabledStore = false;
        }
Exemplo n.º 18
0
        /// <summary>
        ///     Look for entries in the disabled startup backup store.
        /// </summary>
        public IEnumerable <StartupEntry> AddDisableInfo(IList <StartupEntry> existingEntries)
        {
            foreach (var entry in existingEntries)
            {
                yield return(entry);
            }

            using (var regDisKey = RegistryTools.CreateSubKeyRecursively(RegistryDisabledKey.Path))
            {
                var badLocations = new List <string>();
                foreach (var subKeyName in regDisKey.GetSubKeyNames())
                {
                    using (var subKey = regDisKey.OpenSubKey(subKeyName))
                    {
                        if (subKey == null)
                        {
                            continue;
                        }

                        var key     = subKey.GetValue("key") as string;
                        var hkey    = subKey.GetValue("hkey") as string;
                        var item    = subKey.GetValue("item") as string;
                        var command = subKey.GetValue("command") as string;
                        if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(hkey) ||
                            string.IsNullOrEmpty(item) || string.IsNullOrEmpty(command))
                        {
                            continue;
                        }

                        var location = Path.Combine(RegistryTools.GetKeyRoot(hkey, false), key);

                        if (string.IsNullOrEmpty(location.Trim()))
                        {
                            continue;
                        }

                        var runLocation = StartupEntryFactory.RunLocations
                                          .FirstOrDefault(x => PathTools.PathsEqual(location, x.Path));

                        if (runLocation != null)
                        {
                            StartupEntry startupEntry;
                            try
                            {
                                startupEntry = new StartupEntry(runLocation, item, command)
                                {
                                    DisabledStore = true
                                };
                            }
                            catch
                            {
                                badLocations.Add(location);
                                continue;
                            }
                            yield return(startupEntry);
                        }
                        else
                        {
                            badLocations.Add(location);
                        }
                    }
                }

#if DEBUG
                if (badLocations.Any())
                {
                    throw new InvalidDataException(Localisation.Error_InvalidRegKeys + "\n"
                                                   +
                                                   string.Join("\n", badLocations.Distinct().OrderBy(x => x).ToArray()));
                }
#endif
            }

            using (var hddDisKey = RegistryTools.CreateSubKeyRecursively(DriveDisabledKey.Path))
            {
                foreach (var subKeyName in hddDisKey.GetSubKeyNames())
                {
                    using (var subKey = hddDisKey.OpenSubKey(subKeyName))
                    {
                        if (subKey == null)
                        {
                            continue;
                        }

                        var path     = subKey.GetValue("path") as string;
                        var location = subKey.GetValue("location") as string;
                        var backup   = subKey.GetValue("backup") as string;
                        var command  = subKey.GetValue("command") as string;
                        if (backup == null || location == null || path == null || command == null)
                        {
                            continue;
                        }

                        var runLocation =
                            StartupEntryFactory.RunLocations.FirstOrDefault(x => PathTools.PathsEqual(x.Path, location));

                        yield return(new StartupEntry(runLocation, Path.GetFileName(path), command)
                        {
                            BackupPath = backup,
                            DisabledStore = true
                        });
                    }
                }
            }
        }
Exemplo n.º 19
0
        /// <summary>
        ///     Create a new record in the appropriate disabled entry store. If the entry already exists it is overwritten.
        /// </summary>
        /// <param name="startupEntry">Startup entry to create the record for</param>
        /// <param name="newEntryPath">Full path to the new backup file</param>
        private static void CreateDisabledEntry(StartupEntry startupEntry, string newEntryPath)
        {
            using (var disabledStartupEntryStore = RegistryTools.OpenRegistryKey(
                       startupEntry.IsRegKey ? RegistryDisabledKey.Path : DriveDisabledKey.Path, true))
            {
                var disabledSubKeyName = startupEntry.IsRegKey
                    ? startupEntry.EntryLongName
                    : startupEntry.FullLongName.Replace('\\', '^');
                var disabledSubkeyKey =
                    disabledStartupEntryStore.GetSubKeyNames()
                    .FirstOrDefault(x => disabledSubKeyName.Equals(x, StringComparison.InvariantCultureIgnoreCase));

                // Clean up old disabled entry if any
                if (!string.IsNullOrEmpty(disabledSubkeyKey))
                {
                    disabledStartupEntryStore.DeleteSubKey(disabledSubkeyKey);
                }

                using (
                    var storeSubkey = disabledStartupEntryStore.CreateSubKey(disabledSubKeyName,
                                                                             RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    if (storeSubkey == null)
                    {
                        return;
                    }

                    if (startupEntry.IsRegKey)
                    {
                        storeSubkey.SetValue("key", RegistryTools.StripKeyRoot(startupEntry.ParentLongName),
                                             RegistryValueKind.String);
                        storeSubkey.SetValue("item", startupEntry.EntryLongName, RegistryValueKind.String);
                        storeSubkey.SetValue("hkey", RegistryTools.GetKeyRoot(startupEntry.ParentLongName, true),
                                             RegistryValueKind.String);
                        storeSubkey.SetValue("inimapping", 0, RegistryValueKind.String);
                    }
                    else
                    {
                        storeSubkey.SetValue("item",
                                             Path.GetFileNameWithoutExtension(startupEntry.EntryLongName) ?? string.Empty,
                                             RegistryValueKind.String);
                        storeSubkey.SetValue("path", startupEntry.FullLongName, RegistryValueKind.String);
                        storeSubkey.SetValue("location", startupEntry.ParentLongName, RegistryValueKind.String);
                        storeSubkey.SetValue("backup", newEntryPath, RegistryValueKind.String);
                        storeSubkey.SetValue("backupExtension", BackupExtension, RegistryValueKind.String);
                    }

                    // Command stays the same for both
                    storeSubkey.SetValue("command", startupEntry.Command, RegistryValueKind.String);

                    // Set the disable date
                    var now = DateTime.Now;
                    storeSubkey.SetValue("YEAR", now.Year, RegistryValueKind.DWord);
                    storeSubkey.SetValue("MONTH", now.Month, RegistryValueKind.DWord);
                    storeSubkey.SetValue("DAY", now.Day, RegistryValueKind.DWord);
                    storeSubkey.SetValue("HOUR", now.Hour, RegistryValueKind.DWord);
                    storeSubkey.SetValue("MINUTE", now.Minute, RegistryValueKind.DWord);
                    storeSubkey.SetValue("SECOND", now.Second, RegistryValueKind.DWord);
                }
            }
        }
 public void Disable(StartupEntry startupEntry)
 {
     SetDisabled(startupEntry, true);
 }
 public void Enable(StartupEntry startupEntry)
 {
     SetDisabled(startupEntry, false);
 }
Exemplo n.º 22
0
 /// <summary>
 ///     Create backup store path for the link. The backup extension is appended as well.
 ///     Works only for links, returns garbage for registry values.
 /// </summary>
 public string GetDisabledEntryPath(StartupEntry startupEntry)
 {
     return(string.IsNullOrEmpty(startupEntry.BackupPath)
         ? CreateDisabledEntryPath(startupEntry)
         : startupEntry.BackupPath);
 }
 public string GetDisabledEntryPath(StartupEntry startupEntry)
 {
     return(startupEntry.FullLongName);
 }