Provides convenience helper methods for registry access.
Beispiel #1
0
        /// <summary>
        /// Retrieves a list of file associations and programmatic identifiers the registry.
        /// </summary>
        private static (ComparableTuple <string>[] fileAssocs, string[] progIDs) GetFileAssocData()
        {
            var fileAssocsList = new List <ComparableTuple <string> >();
            var progIDsList    = new List <string>();

            foreach (string keyName in Registry.ClassesRoot.GetSubKeyNames())
            {
                if (keyName.StartsWith("."))
                {
                    using var assocKey = Registry.ClassesRoot.OpenSubKey(keyName);
                    // Get the main ProgID
                    string assocValue = assocKey?.GetValue("")?.ToString();
                    if (string.IsNullOrEmpty(assocValue))
                    {
                        continue;
                    }
                    fileAssocsList.Add(new ComparableTuple <string>(keyName, assocValue));

                    // Get additional ProgIDs
                    fileAssocsList.AddRange(RegUtils.GetValueNames(assocKey, FileType.RegSubKeyOpenWith).Select(progID => new ComparableTuple <string>(keyName, progID)));
                }
                else
                {
                    progIDsList.Add(keyName);
                }
            }

            return(fileAssocsList.ToArray(), progIDsList.ToArray());
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves a list of file assocations and programatic indentifiers the registry.
        /// </summary>
        private static void GetFileAssocData(out ComparableTuple <string>[] fileAssocs, out string[] progIDs)
        {
            var fileAssocsList = new List <ComparableTuple <string> >();
            var progIDsList    = new List <string>();

            foreach (string keyName in Registry.ClassesRoot.GetSubKeyNames())
            {
                if (keyName.StartsWith("."))
                {
                    using (var assocKey = Registry.ClassesRoot.OpenSubKey(keyName))
                    {
                        if (assocKey == null)
                        {
                            continue;
                        }

                        // Get the main ProgID
                        fileAssocsList.Add(new ComparableTuple <string>(keyName, assocKey.GetValue("", "").ToString()));

                        // Get additional ProgIDs
                        fileAssocsList.AddRange(RegUtils.GetValueNames(assocKey, FileType.RegSubKeyOpenWith).Select(progID => new ComparableTuple <string>(keyName, progID)));
                    }
                }
                else
                {
                    progIDsList.Add(keyName);
                }
            }

            fileAssocs = fileAssocsList.ToArray();
            progIDs    = progIDsList.ToArray();
        }
Beispiel #3
0
        /// <summary>
        /// Retrieves a list of AutoPlay associations from the registry.
        /// </summary>
        /// <param name="hive">The registry hive to search in (usually HKCU or HKLM).</param>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        private static ComparableTuple <string>[] GetAutoPlayAssocs(RegistryKey hive)
        {
            using var eventsKey = hive.OpenSubKey(AutoPlay.RegKeyAssocs);
            if (eventsKey == null)
            {
                return(new ComparableTuple <string> [0]);
            }

            return((
                       from eventName in eventsKey.GetSubKeyNames()
                       from handlerName in RegUtils.GetValueNames(eventsKey, eventName)
                       select new ComparableTuple <string>(eventName, handlerName)).ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Retrieves data about multiple verbs (executable commands) from the registry.
        /// </summary>
        /// <param name="typeKey">The registry key containing information about the file type / protocol the verbs belong to.</param>
        /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
        /// <returns>A list of detected <see cref="Verb"/>.</returns>
        private static IEnumerable <Verb> GetVerbs(RegistryKey typeKey, CommandMapper commandMapper)
        {
            #region Sanity checks
            if (typeKey == null)
            {
                throw new ArgumentNullException(nameof(typeKey));
            }
            if (commandMapper == null)
            {
                throw new ArgumentNullException(nameof(commandMapper));
            }
            #endregion

            return(RegUtils.GetSubKeyNames(typeKey, "shell").Select(verbName => GetVerb(typeKey, commandMapper, verbName)).WhereNotNull());
        }
Beispiel #5
0
        /// <summary>
        /// Retrieves a list of service associations from the registry.
        /// </summary>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        private static ComparableTuple <string>[] GetServiceAssocs()
        {
            using var clientsKey = Registry.LocalMachine.OpenSubKey(DefaultProgram.RegKeyMachineClients);
            if (clientsKey == null)
            {
                return(new ComparableTuple <string> [0]);
            }

            return((
                       from serviceName in clientsKey.GetSubKeyNames()
                       // ReSharper disable AccessToDisposedClosure
                       from clientName in RegUtils.GetSubKeyNames(clientsKey, serviceName)
                       // ReSharper restore AccessToDisposedClosure
                       select new ComparableTuple <string>(serviceName, clientName)).ToArray());
        }
Beispiel #6
0
        /// <summary>
        /// Stores information about the current state of the registry in a snapshot.
        /// </summary>
        /// <exception cref="IOException">There was an error accessing the registry.</exception>
        /// <exception cref="UnauthorizedAccessException">Read access to the registry was not permitted.</exception>
        private void TakeRegistry()
        {
            ServiceAssocs           = GetServiceAssocs();
            AutoPlayHandlersUser    = RegUtils.GetSubKeyNames(Registry.CurrentUser, AutoPlay.RegKeyHandlers);
            AutoPlayHandlersMachine = RegUtils.GetSubKeyNames(Registry.LocalMachine, AutoPlay.RegKeyHandlers);
            AutoPlayAssocsUser      = GetAutoPlayAssocs(Registry.CurrentUser);
            AutoPlayAssocsMachine   = GetAutoPlayAssocs(Registry.LocalMachine);
            (FileAssocs, ProgIDs)   = GetFileAssocData();
            ProtocolAssocs          = GetProtocolAssoc();
            ClassIDs = RegUtils.GetSubKeyNames(Registry.ClassesRoot, ComServer.RegKeyClassesIDs);
            RegisteredApplications = RegUtils.GetValueNames(Registry.LocalMachine, AppRegistration.RegKeyMachineRegisteredApplications);

            ContextMenuFiles           = RegUtils.GetSubKeyNames(Registry.ClassesRoot, ContextMenu.RegKeyClassesFiles + @"\shell");
            ContextMenuExecutableFiles = RegUtils.GetSubKeyNames(Registry.ClassesRoot, ContextMenu.RegKeyClassesExecutableFiles + @"\shell");
            ContextMenuDirectories     = RegUtils.GetSubKeyNames(Registry.ClassesRoot, ContextMenu.RegKeyClassesDirectories + @"\shell");
            ContextMenuAll             = RegUtils.GetSubKeyNames(Registry.ClassesRoot, ContextMenu.RegKeyClassesAll + @"\shell");
        }