Beispiel #1
0
    /// <summary>
    /// Registers a URL protocol in the current system.
    /// </summary>
    /// <param name="target">The application being integrated.</param>
    /// <param name="urlProtocol">The URL protocol to register.</param>
    /// <param name="machineWide">Register the URL protocol machine-wide instead of just for the current user.</param>
    /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
    /// <param name="accessPoint">Indicates that the handler shall become the default handler for the protocol.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Register(FeedTarget target, Model.Capabilities.UrlProtocol urlProtocol, IIconStore iconStore, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (urlProtocol == null)
        {
            throw new ArgumentNullException(nameof(urlProtocol));
        }
        if (iconStore == null)
        {
            throw new ArgumentNullException(nameof(iconStore));
        }
        #endregion

        using var classesKey = RegistryClasses.OpenHive(machineWide);

        if (urlProtocol.KnownPrefixes.Count == 0)
        {
            if (accessPoint)
            { // Can only be registered invasively by registering protocol ProgID (will replace existing and become default)
                using var progIDKey = classesKey.CreateSubKeyChecked(urlProtocol.ID);
                progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID);
                RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                progIDKey.SetValue(ProtocolIndicator, "");
            }
        }
        else
        { // Can be registered non-invasively by registering custom ProgID (without becoming default)
            using (var progIDKey = classesKey.CreateSubKeyChecked(RegistryClasses.Prefix + urlProtocol.ID))
            {
                progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID);
                progIDKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
                RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                progIDKey.SetValue(ProtocolIndicator, "");
            }

            if (accessPoint)
            {
                foreach (var prefix in urlProtocol.KnownPrefixes)
                {
                    if (WindowsUtils.IsWindowsVista && !machineWide)
                    {
                        using var userChoiceKey = Registry.CurrentUser.CreateSubKeyChecked($@"{RegKeyUserVistaUrlAssoc}\{prefix.Value}\UserChoice");
                        userChoiceKey.SetValue("ProgID", RegistryClasses.Prefix + urlProtocol.ID);
                    }
                    else
                    {
                        // Setting default invasively by registering protocol ProgID
                        using var progIDKey = classesKey.CreateSubKeyChecked(prefix.Value);
                        RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide);
                        progIDKey.SetValue(ProtocolIndicator, "");
                    }
                }
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// Unregisters a URL protocol in the current system.
    /// </summary>
    /// <param name="urlProtocol">The URL protocol to remove.</param>
    /// <param name="machineWide">Unregister the URL protocol machine-wide instead of just for the current user.</param>
    /// <param name="accessPoint">Indicates that the handler was the default handler for the protocol.</param>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Unregister(Model.Capabilities.UrlProtocol urlProtocol, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (urlProtocol == null)
        {
            throw new ArgumentNullException(nameof(urlProtocol));
        }
        #endregion

        using var classesKey = RegistryClasses.OpenHive(machineWide);

        if (urlProtocol.KnownPrefixes.Count == 0)
        {
            if (accessPoint) // Was registered invasively by registering protocol ProgID
            {
                classesKey.DeleteSubKeyTree(urlProtocol.ID, throwOnMissingSubKey: false);
            }
        }
        else
        { // Was registered non-invasively by registering custom ProgID
            if (accessPoint)
            {
                // TODO: Restore previous default
                // foreach (var prefix in urlProtocol.KnownPrefixes)
                // {
                // }
            }

            // Remove appropriate purpose flag and check if there are others
            bool otherFlags;
            using (var progIDKey = classesKey.OpenSubKey(RegistryClasses.Prefix + urlProtocol.ID, writable: true))
            {
                if (progIDKey == null)
                {
                    otherFlags = false;
                }
                else
                {
                    progIDKey.DeleteValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, throwOnMissingValue: false);
                    otherFlags = progIDKey.GetValueNames().Any(name => name.StartsWith(RegistryClasses.PurposeFlagPrefix));
                }
            }

            // Delete ProgID if there are no other references
            if (!otherFlags)
            {
                classesKey.DeleteSubKeyTree(RegistryClasses.Prefix + urlProtocol.ID, throwOnMissingSubKey: false);
            }
        }
        #endregion
    }
Beispiel #3
0
    /// <summary>
    /// Removes an AutoPlay handler registration from the current system.
    /// </summary>
    /// <param name="autoPlay">The AutoPlay handler information to be removed.</param>
    /// <param name="machineWide">Remove the handler machine-wide instead of just for the current user.</param>
    /// <param name="accessPoint">Indicates that the handler should was the default handler for all <see cref="Model.Capabilities.AutoPlay.Events"/>.</param>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Unregister(Model.Capabilities.AutoPlay autoPlay, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (autoPlay == null)
        {
            throw new ArgumentNullException(nameof(autoPlay));
        }
        #endregion

        var    hive        = machineWide ? Registry.LocalMachine : Registry.CurrentUser;
        string handlerName = RegistryClasses.Prefix + autoPlay.ID;
        string progId      = RegistryClasses.Prefix + "AutoPlay." + autoPlay.ID;

        if (accessPoint)
        {
            // TODO: Restore previous default
        }

        // Remove appropriate purpose flag and check if there are others
        bool otherFlags;
        using (var handlerKey = hive.OpenSubKey($@"{RegKeyHandlers}\{handlerName}", writable: true))
        {
            if (handlerKey == null)
            {
                otherFlags = false;
            }
            else
            {
                handlerKey.DeleteValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, throwOnMissingValue: false);
                otherFlags = handlerKey.GetValueNames().Any(name => name.StartsWith(RegistryClasses.PurposeFlagPrefix));
            }
        }

        // Delete handler key and ProgID if there are no other references
        if (!otherFlags)
        {
            foreach (var autoPlayEvent in autoPlay.Events.Except(x => string.IsNullOrEmpty(x.Name)))
            {
                using var eventKey = hive.OpenSubKey($@"{RegKeyAssocs}\{autoPlayEvent.Name}", writable: true);
                eventKey?.DeleteValue(handlerName, throwOnMissingValue: false);
            }

            hive.DeleteSubKeyTree($@"{RegKeyHandlers}\{handlerName}", throwOnMissingSubKey: false);

            using var classesKey = RegistryClasses.OpenHive(machineWide);
            classesKey.DeleteSubKeyTree(progId, throwOnMissingSubKey: false);
        }
    }
Beispiel #4
0
    /// <summary>
    /// Registers an application as a candidate for a default program for some service in the current system. This can only be applied machine-wide, not per user.
    /// </summary>
    /// <param name="target">The application being integrated.</param>
    /// <param name="defaultProgram">The default program information to be registered.</param>
    /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
    /// <param name="accessPoint">Indicates that the program should be set as the current default for the service it provides.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Register(FeedTarget target, Model.Capabilities.DefaultProgram defaultProgram, IIconStore iconStore, bool accessPoint = false)
    {
        #region Sanity checks
        if (defaultProgram == null)
        {
            throw new ArgumentNullException(nameof(defaultProgram));
        }
        if (iconStore == null)
        {
            throw new ArgumentNullException(nameof(iconStore));
        }
        #endregion

        using var serviceKey = Registry.LocalMachine.CreateSubKeyChecked($@"{RegKeyMachineClients}\{defaultProgram.Service}");
        using (var appKey = serviceKey.CreateSubKeyChecked(defaultProgram.ID))
        {
            appKey.SetValue("", target.Feed.Name);
            appKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
            RegistryClasses.Register(appKey, target, defaultProgram, iconStore, machineWide: true);

            // Set callbacks for Windows SPAD
            using (var installInfoKey = appKey.CreateSubKeyChecked(RegSubKeyInstallInfo))
            {
                string exePath = Path.Combine(Locations.InstallBase, "0install-win.exe");
                installInfoKey.SetValue(RegValueReinstallCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", "defaults", target.Uri.ToStringRfc() }.JoinEscapeArguments());
                installInfoKey.SetValue(RegValueShowIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", MenuEntry.TagName, "--add", DesktopIcon.TagName, target.Uri.ToStringRfc() }.JoinEscapeArguments());
                installInfoKey.SetValue(RegValueHideIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--remove", MenuEntry.TagName, "--remove", DesktopIcon.TagName, target.Uri.ToStringRfc() }.JoinEscapeArguments());
                installInfoKey.SetValue(RegValueIconsVisible, 0, RegistryValueKind.DWord);
            }

            if (defaultProgram.Service == Model.Capabilities.DefaultProgram.ServiceMail)
            {
                var mailToProtocol = new Model.Capabilities.UrlProtocol {
                    Verbs = { new Verb {
                                  Name = Verb.NameOpen
                              } }
                };
                using var mailToKey = appKey.CreateSubKeyChecked(@"Protocols\mailto");
                RegistryClasses.Register(mailToKey, target, mailToProtocol, iconStore, machineWide: true);
            }
        }

        if (accessPoint)
        {
            serviceKey.SetValue("", defaultProgram.ID);
        }
    }
Beispiel #5
0
    /// <summary>
    /// Adds an AutoPlay handler registration to the current system.
    /// </summary>
    /// <param name="target">The application being integrated.</param>
    /// <param name="autoPlay">The AutoPlay handler information to be applied.</param>
    /// <param name="machineWide">Register the handler machine-wide instead of just for the current user.</param>
    /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
    /// <param name="accessPoint">Indicates that the handler should become the default handler for all <see cref="Model.Capabilities.AutoPlay.Events"/>.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Register(FeedTarget target, Model.Capabilities.AutoPlay autoPlay, IIconStore iconStore, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (autoPlay == null)
        {
            throw new ArgumentNullException(nameof(autoPlay));
        }
        if (iconStore == null)
        {
            throw new ArgumentNullException(nameof(iconStore));
        }
        #endregion

        string handlerName = RegistryClasses.Prefix + autoPlay.ID;
        string progId      = RegistryClasses.Prefix + "AutoPlay." + autoPlay.ID;

        using (var classesKey = RegistryClasses.OpenHive(machineWide))
            using (var verbKey = classesKey.CreateSubKeyChecked($@"{progId}\shell\{autoPlay.Verb.Name}"))
                RegistryClasses.Register(verbKey, target, autoPlay.Verb, iconStore, machineWide);

        var hive = machineWide ? Registry.LocalMachine : Registry.CurrentUser;

        using (var handlerKey = hive.CreateSubKeyChecked($@"{RegKeyHandlers}\{handlerName}"))
        {
            handlerKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
            handlerKey.SetValue(RegValueProgID, progId);
            handlerKey.SetValue(RegValueVerb, autoPlay.Verb.Name);
            handlerKey.SetValue(RegValueProvider, autoPlay.Provider);
            handlerKey.SetValue(RegValueDescription, autoPlay.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? autoPlay.Verb.Name);

            var icon = autoPlay.GetIcon(Icon.MimeTypeIco)
                       ?? target.Feed.GetBestIcon(Icon.MimeTypeIco, autoPlay.Verb.Command);
            handlerKey.SetOrDelete(RegValueIcon, icon?.To(x => iconStore.GetFresh(x) + ",0"));
        }

        foreach (var autoPlayEvent in autoPlay.Events.Except(x => string.IsNullOrEmpty(x.Name)))
        {
            using (var eventKey = hive.CreateSubKeyChecked($@"{RegKeyAssocs}\{autoPlayEvent.Name}"))
                eventKey.SetValue(handlerName, "");

            if (accessPoint)
            {
                using var chosenEventKey = hive.CreateSubKeyChecked($@"{RegKeyChosenAssocs}\{autoPlayEvent.Name}");
                chosenEventKey.SetValue("", handlerName);
            }
        }
    }
Beispiel #6
0
    /// <summary>
    /// Registers a file type in the current system.
    /// </summary>
    /// <param name="target">The application being integrated.</param>
    /// <param name="fileType">The file type to register.</param>
    /// <param name="machineWide">Register the file type machine-wide instead of just for the current user.</param>
    /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
    /// <param name="accessPoint">Indicates that the file associations shall become default handlers for their respective types.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Register(FeedTarget target, Model.Capabilities.FileType fileType, IIconStore iconStore, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (fileType == null)
        {
            throw new ArgumentNullException(nameof(fileType));
        }
        if (iconStore == null)
        {
            throw new ArgumentNullException(nameof(iconStore));
        }
        #endregion

        using var classesKey = RegistryClasses.OpenHive(machineWide);

        // Register ProgID
        using (var progIDKey = classesKey.CreateSubKeyChecked(RegistryClasses.Prefix + fileType.ID))
        {
            progIDKey.SetValue("", fileType.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? fileType.ID);
            progIDKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, "");
            RegistryClasses.Register(progIDKey, target, fileType, iconStore, machineWide);
        }

        foreach (var extension in fileType.Extensions.Except(x => string.IsNullOrEmpty(x.Value)))
        {
            // Register extensions
            using (var extensionKey = classesKey.CreateSubKeyChecked(extension.Value))
            {
                extensionKey.SetOrDelete(RegValueContentType, extension.MimeType);
                extensionKey.SetOrDelete(RegValuePerceivedType, extension.PerceivedType);

                using (var openWithKey = extensionKey.CreateSubKeyChecked(RegSubKeyOpenWith))
                    openWithKey.SetValue(RegistryClasses.Prefix + fileType.ID, "");

                if (accessPoint)
                {
                    if (!machineWide && WindowsUtils.IsWindowsVista)
                    { // Windows Vista and later store per-user file extension overrides
                        using var overridesKey         = Registry.CurrentUser.OpenSubKeyChecked(RegKeyOverrides, writable: true);
                        using var extensionOverrideKey = overridesKey.CreateSubKeyChecked(extension.Value);
                        // Only mess with this part of the registry when necessary
                        bool alreadySet;
                        using (var userChoiceKey = extensionOverrideKey.OpenSubKey("UserChoice", writable: false))
                        {
                            if (userChoiceKey == null)
                            {
                                alreadySet = false;
                            }
                            else
                            {
                                alreadySet = ((userChoiceKey.GetValue("Progid") ?? "").ToString() == RegistryClasses.Prefix + fileType.ID);
                            }
                        }

                        if (!alreadySet)
                        {
                            // Must delete and recreate instead of direct modification due to wicked ACLs
                            extensionOverrideKey.DeleteSubKeyTree("UserChoice", throwOnMissingSubKey: false);

                            try
                            {
                                using var userChoiceKey = extensionOverrideKey.CreateSubKeyChecked("UserChoice");
                                userChoiceKey.SetValue("Progid", RegistryClasses.Prefix + fileType.ID);
                            }
                            catch (UnauthorizedAccessException ex)
                            {
                                // Windows may try to prevent modifications to this key
                                Log.Debug(ex);
                            }
                        }
                    }
                    else
                    {
                        extensionKey.SetValue("", RegistryClasses.Prefix + fileType.ID);
                    }
                }
            }

            // Register MIME types
            if (!string.IsNullOrEmpty(extension.MimeType))
            {
                using var mimeKey = classesKey.CreateSubKeyChecked($@"{RegSubKeyMimeType}\{extension.MimeType}");
                mimeKey.SetValue(RegValueExtension, extension.Value);
            }
        }
    }
Beispiel #7
0
    /// <summary>
    /// Unregisters a file type in the current system.
    /// </summary>
    /// <param name="fileType">The file type to remove.</param>
    /// <param name="machineWide">Unregister the file type machine-wide instead of just for the current user.</param>
    /// <param name="accessPoint">Indicates that the file associations were default handlers for their respective types.</param>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
    public static void Unregister(Model.Capabilities.FileType fileType, bool machineWide, bool accessPoint = false)
    {
        #region Sanity checks
        if (fileType == null)
        {
            throw new ArgumentNullException(nameof(fileType));
        }
        #endregion

        using var classesKey = RegistryClasses.OpenHive(machineWide);
        foreach (var extension in fileType.Extensions.Except(extension => string.IsNullOrEmpty(extension.Value)))
        {
            // Unregister MIME types
            // if (!string.IsNullOrEmpty(extension.MimeType))
            // {
            //     using (var extensionKey = classesKey.CreateSubKeyChecked(extension.Value))
            //     {
            //         // TODO: Restore previous default
            //         extensionKey.SetValue("", fileType.PreviousID);
            //     }
            //
            //     if (!machineWide && WindowsUtils.IsWindowsVista && !WindowsUtils.IsWindows8)
            //     {
            //         // Windows Vista and later store per-user file extension overrides, Windows 8 blocks programmatic modification with hash values
            //         using (var overridesKey = hive.OpenSubKey(RegKeyOverrides, true))
            //         using (var extensionOverrideKey = overridesKey.CreateSubKeyChecked(extension.Value))
            //         {
            //             // Must delete and recreate instead of direct modification due to wicked ACLs
            //             extensionOverrideKey.DeleteSubKeyTree("UserChoice", throwOnMissingSubKey: false);
            //             using (var userChoiceKey = extensionOverrideKey.CreateSubKeyChecked("UserChoice"))
            //                 userChoiceKey.SetValue("ProgID", fileType.PreviousID);
            //         }
            //     }
            // }

            // Unregister extensions
            using var extensionKey = classesKey.OpenSubKey(extension.Value, writable: true);
            if (extensionKey != null)
            {
                using var openWithKey = extensionKey.OpenSubKey(RegSubKeyOpenWith, writable: true);
                openWithKey?.DeleteValue(RegistryClasses.Prefix + fileType.ID, throwOnMissingValue: false);
            }

            if (accessPoint)
            {
                // TODO: Restore previous default
            }
        }

        // Remove appropriate purpose flag and check if there are others
        bool otherFlags;
        using (var progIDKey = classesKey.OpenSubKey(RegistryClasses.Prefix + fileType.ID, writable: true))
        {
            if (progIDKey == null)
            {
                otherFlags = false;
            }
            else
            {
                progIDKey.DeleteValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, throwOnMissingValue: false);
                otherFlags = progIDKey.GetValueNames().Any(name => name.StartsWith(RegistryClasses.PurposeFlagPrefix));
            }
        }

        // Delete ProgID if there are no other references
        if (!otherFlags)
        {
            classesKey.DeleteSubKeyTree(RegistryClasses.Prefix + fileType.ID, throwOnMissingSubKey: false);
        }
    }
Beispiel #8
0
 private static string GetLaunchCommandLine(Verb verb)
 => RegistryClasses.GetLaunchCommandLine(new FeedTarget(Fake.Feed1Uri, Fake.Feed), verb, new Mock <IIconStore>().Object, machineWide: false);