示例#1
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 occurs while writing to the filesystem.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="fileType"/> is invalid.</exception>
        public static void Unregister([NotNull] Store.Model.Capabilities.FileType fileType, bool machineWide, bool accessPoint = false)
        {
            #region Sanity checks
            if (fileType == null)
            {
                throw new ArgumentNullException("fileType");
            }
            #endregion

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

            // TODO: Implement
        }
示例#2
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="handler">A callback object used when the the user is to be informed about the progress of long-running operations such as downloads.</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 occurs while writing to the filesystem.</exception>
        /// <exception cref="WebException">A problem occured while downloading additional data (such as icons).</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
        /// <exception cref="InvalidDataException">The data in <paramref name="fileType"/> is invalid.</exception>
        public static void Register(FeedTarget target, [NotNull] Store.Model.Capabilities.FileType fileType, bool machineWide, [NotNull] ITaskHandler handler, bool accessPoint = false)
        {
            #region Sanity checks
            if (fileType == null)
            {
                throw new ArgumentNullException(nameof(fileType));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

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

            // TODO: Implement
        }
示例#3
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 occurs 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([NotNull] Store.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");
            }

            var hive = machineWide ? Registry.LocalMachine : Registry.CurrentUser;
            using (var classesKey = hive.OpenSubKeyChecked(RegKeyClasses, writable: true))
            {
                foreach (var extension in fileType.Extensions.Except(extension => string.IsNullOrEmpty(extension.Value)))
                {
                    // Unegister 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.DeleteSubKey("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(RegKeyPrefix + 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(RegKeyPrefix + fileType.ID, writable: true))
                {
                    if (progIDKey == null)
                    {
                        otherFlags = false;
                    }
                    else
                    {
                        progIDKey.DeleteValue(accessPoint ? PurposeFlagAccessPoint : PurposeFlagCapability, throwOnMissingValue: false);
                        otherFlags = progIDKey.GetValueNames().Any(name => name.StartsWith(PurposeFlagPrefix));
                    }
                }

                // Delete ProgID if there are no other references
                if (!otherFlags)
                {
                    try
                    {
                        classesKey.DeleteSubKeyTree(RegKeyPrefix + fileType.ID);
                    }

                    #region Error handling
                    catch (ArgumentException)
                    {
                        // Ignore missing registry keys
                    }
                    #endregion
                }
            }
        }
示例#4
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="handler">A callback object used when the the user is to be informed about the progress of long-running operations such as downloads.</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 occurs while writing to the filesystem or registry.</exception>
        /// <exception cref="WebException">A problem occured 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, [NotNull] Store.Model.Capabilities.FileType fileType, bool machineWide, [NotNull] ITaskHandler handler, bool accessPoint = false)
        {
            #region Sanity checks
            if (fileType == null)
            {
                throw new ArgumentNullException(nameof(fileType));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

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

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

            // Register ProgID
            using (var progIDKey = hive.CreateSubKeyChecked(RegKeyClasses + @"\" + RegKeyPrefix + fileType.ID))
            {
                // Add flag to remember whether created for capability or access point
                progIDKey.SetValue(accessPoint ? PurposeFlagAccessPoint : PurposeFlagCapability, "");

                RegisterVerbCapability(progIDKey, target, fileType, machineWide, handler);
            }

            using (var classesKey = hive.OpenSubKeyChecked(RegKeyClasses, writable: true))
            {
                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(RegKeyPrefix + fileType.ID, "");

                        if (accessPoint)
                        {
                            if (!machineWide && WindowsUtils.IsWindowsVista)
                            { // Windows Vista and later store per-user file extension overrides
                                using (var overridesKey = hive.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() == RegKeyPrefix + fileType.ID);
                                            }
                                        }

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

                                            using (var userChoiceKey = extensionOverrideKey.CreateSubKeyChecked("UserChoice"))
                                                userChoiceKey.SetValue("Progid", RegKeyPrefix + fileType.ID);
                                        }
                                    }
                            }
                            else
                            {
                                extensionKey.SetValue("", RegKeyPrefix + fileType.ID);
                            }
                        }
                    }

                    // Register MIME types
                    if (!string.IsNullOrEmpty(extension.MimeType))
                    {
                        using (var mimeKey = classesKey.CreateSubKeyChecked(RegSubKeyMimeType + @"\" + extension.MimeType))
                            mimeKey.SetValue(RegValueExtension, extension.Value);
                    }
                }
            }
        }