コード例 #1
0
        /// <summary>
        /// Removes the associations/registrations of the specified file types (extensions) from a specified application.
        /// </summary>
        /// <param name="company">The company name of the software in question.</param>
        /// <param name="applicationName">Name of the application.</param>
        /// <returns><c>true</c> if the operation was successful, <c>false</c> otherwise.</returns>
        // ReSharper disable once UnusedMember.Global, this is a class to be copied and used in another program..
        public static bool UnAssociateFiles(string company, string applicationName)
        {
            var result = false;

            try
            {
                int valueCount;

                var appRegistryTree = string.Concat(@"SOFTWARE\", company, @"\", applicationName);

                // register the registered file types..
                using (var key = CommonCalls.OpenOrCreateKeyHKLM(appRegistryTree))
                {
                    var registryValue      = key.GetValue("Associations").ToString();
                    var associationStrings = registryValue.Split(':');

                    foreach (var associationString in associationStrings)
                    {
                        var extension = associationString.Split('|')[0];

                        if (extension.StartsWith("(")) // format: (.m3u/.m3u8)|Music playlist files..
                        {
                            string[] innerExtensions = extension.TrimStart('(').TrimEnd(')').Split('/');
                            foreach (var innerExtension in innerExtensions)
                            {
                                result |= UnAssociate(innerExtension, applicationName);
                            }
                        }
                        else // format: .txt|Text files..
                        {
                            result |= UnAssociate(extension, applicationName);
                        }
                    }

                    valueCount = key.ValueCount;
                }

                if (valueCount == 0)
                {
                    Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(appRegistryTree);
                }

                return(result);
            }
            catch (Exception ex)
            {
                // report the exception..
                ReportExceptionAction?.Invoke(ex);
                return(false);
            }
        }
コード例 #2
0
        // ReSharper disable once CommentTypo
        /// <summary>
        /// Associates/registers the specified file types (extensions) to a specified application.
        /// </summary>
        /// <param name="company">The company name of the software in question.</param>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="applicationExecutableFile">The application executable file.</param>
        /// <param name="associationList">A semi-colon delimited string of file extensions and their names (.ext;name|.ext2;name2).</param>
        /// <param name="rootDefault">if set to <c>true</c> HKCR (HKey Classes Root) default value is set for the specified application.</param>
        /// <param name="iconIndex">Index of the icon to use from the <see paramref="applicationExecutableFile"/>.</param>
        /// <returns><c>true</c> if the operation was successful, <c>false</c> otherwise.</returns>
        // ReSharper disable once UnusedMember.Global, this is a class to be copied and used in another program..
        public static bool AssociateFiles(string company, string applicationName, string applicationExecutableFile, string associationList,
                                          bool rootDefault, int iconIndex = 0)
        {
            var result = false;

            try
            {
                var appRegistryTree = string.Concat(@"SOFTWARE\", company, @"\", applicationName);

                // register the registered file types..
                using (var key = CommonCalls.OpenOrCreateKeyHKLM(appRegistryTree))
                {
                    key.SetValue("Associations", associationList);
                }

                var associationStrings = associationList.Split(':');

                foreach (var associationString in associationStrings)
                {
                    var associationData = associationString.Split('|');

                    if (associationData[0].StartsWith("(")) // format: (.m3u/.m3u8)|Music playlist files..
                    {
                        string[] innerExtensions = associationData[0].TrimStart('(').TrimEnd(')').Split('/');
                        foreach (var innerExtension in innerExtensions)
                        {
                            result |= Associate(applicationExecutableFile, innerExtension, applicationName, rootDefault,
                                                associationData[1], iconIndex);
                        }
                    }
                    else // format: .txt|Text files..
                    {
                        result |= Associate(applicationExecutableFile, associationData[0], applicationName, rootDefault,
                                            associationData[1], iconIndex);
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                // report the exception..
                ReportExceptionAction?.Invoke(ex);
                return(false);
            }
        }