示例#1
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
                        });
                    }
                }
            }
        }
示例#2
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);
                }
            }
        }