Ejemplo n.º 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>
        /// <exception cref="InvalidDataException">The data in <paramref name="urlProtocol"/> is invalid.</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

            if (string.IsNullOrEmpty(urlProtocol.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            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, "");
                        }
                    }
                }
            }
        }
Ejemplo n.º 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>
        /// <exception cref="InvalidDataException">The data in <paramref name="urlProtocol"/> is invalid.</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

            if (string.IsNullOrEmpty(urlProtocol.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            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)
                {
                    foreach (var prefix in urlProtocol.KnownPrefixes)
                    {
                        // TODO: Restore previous default
                    }
                }

                // 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
        }
Ejemplo n.º 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>
        /// <exception cref="InvalidDataException">The data in <paramref name="autoPlay"/> is invalid.</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

            if (string.IsNullOrEmpty(autoPlay.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            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);
            }
        }
Ejemplo n.º 4
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>
        /// <exception cref="InvalidDataException">The data in <paramref name="autoPlay"/> is invalid.</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

            if (string.IsNullOrEmpty(autoPlay.ID))
            {
                throw new InvalidDataException("Missing ID");
            }
            if (string.IsNullOrEmpty(autoPlay.Provider))
            {
                throw new InvalidDataException("Missing provider");
            }

            var verb = autoPlay.Verb;
            if (verb == null)
            {
                throw new InvalidDataException("Missing verb");
            }
            if (string.IsNullOrEmpty(verb.Name))
            {
                throw new InvalidDataException("Missing verb name");
            }

            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\{verb.Name}"))
                    RegistryClasses.Register(verbKey, target, 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, verb.Name);
                handlerKey.SetValue(RegValueProvider, autoPlay.Provider);
                handlerKey.SetValue(RegValueDescription, autoPlay.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? verb.Name);

                var icon = autoPlay.GetIcon(Icon.MimeTypeIco) ?? target.Feed.GetIcon(Icon.MimeTypeIco, verb.Command);
                if (icon != null)
                {
                    handlerKey.SetValue(RegValueIcon, iconStore.GetPath(icon) + ",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);
                }
            }
        }
Ejemplo n.º 5
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>
        /// <exception cref="InvalidDataException">The data in <paramref name="fileType"/> is invalid.</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

            if (string.IsNullOrEmpty(fileType.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            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))
                {
                    if (!string.IsNullOrEmpty(extension.MimeType))
                    {
                        extensionKey.SetValue(RegValueContentType, extension.MimeType);
                    }
                    if (!string.IsNullOrEmpty(extension.PerceivedType))
                    {
                        extensionKey.SetValue(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);
                }
            }
        }
Ejemplo n.º 6
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>
        /// <exception cref="InvalidDataException">The data in <paramref name="fileType"/> is invalid.</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

            if (string.IsNullOrEmpty(fileType.ID))
            {
                throw new InvalidDataException("Missing ID");
            }

            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);
            }
        }