Ejemplo n.º 1
0
    /// <summary>
    /// Collects data about URL protocol handlers indicated by registered application capabilities.
    /// </summary>
    /// <param name="capsKey">A registry key containing capability information for a registered application.</param>
    /// <param name="commandMapper">Provides best-match command-line to <see cref="Command"/> mapping.</param>
    /// <param name="capabilities">The capability list to add the collected data to.</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 void CollectProtocolAssocsEx(RegistryKey capsKey, CommandMapper commandMapper, CapabilityList capabilities)
    {
        #region Sanity checks
        if (capsKey == null)
        {
            throw new ArgumentNullException(nameof(capsKey));
        }
        if (commandMapper == null)
        {
            throw new ArgumentNullException(nameof(commandMapper));
        }
        if (capabilities == null)
        {
            throw new ArgumentNullException(nameof(capabilities));
        }
        #endregion

        using var urlAssocKey = capsKey.OpenSubKey(DesktopIntegration.Windows.AppRegistration.RegSubKeyUrlAssocs);
        if (urlAssocKey == null)
        {
            return;
        }

        foreach (string protocol in urlAssocKey.GetValueNames())
        {
            string?progID = urlAssocKey.GetValue(protocol)?.ToString();
            if (string.IsNullOrEmpty(progID))
            {
                continue;
            }
            using var progIDKey = Registry.ClassesRoot.OpenSubKey(progID);
            if (progIDKey == null)
            {
                continue;
            }

            var prefix = new KnownProtocolPrefix {
                Value = protocol
            };
            var existing = capabilities.GetCapability <UrlProtocol>(progID);
            if (existing == null)
            {
                var capability = new UrlProtocol
                {
                    ID            = progID,
                    Descriptions  = { progIDKey.GetValue("", defaultValue: "")?.ToString() ?? "" },
                    KnownPrefixes = { prefix }
                };
                capability.Verbs.AddRange(GetVerbs(progIDKey, commandMapper));
                capabilities.Entries.Add(capability);
            }
            else
            {
                existing.KnownPrefixes.Add(prefix);
            }
        }
    }