Exemplo n.º 1
0
        /// <summary>
        /// Gets the file association information.
        /// </summary>
        /// <param name="fileExtension">The file extension part.</param>
        /// <returns>The file association information; or null, if not found or failure.</returns>
        public static FileAssociationInfo GetFileAssociationInfo(string fileExtension)
        {
            fileExtension = fileExtension?.Trim()?.ToLowerInvariant();
            if (string.IsNullOrEmpty(fileExtension))
            {
                return(null);
            }
            var info = new FileAssociationInfo
            {
                FileExtension = fileExtension
            };

            using var ext = RegistryUtility.TryOpenSubKey(Reg.ClassesRoot, fileExtension);
            string h;

            if (ext == null)
            {
                if (fileExtension.IndexOf('/') < 1)
                {
                    return(info);
                }
                using var mime = RegistryUtility.TryOpenSubKey(Reg.ClassesRoot, "MIME\\Database\\Content Type\\" + fileExtension);
                if (mime == null)
                {
                    return(info);
                }
                info.ContentType   = fileExtension;
                fileExtension      = RegistryUtility.TryGetStringValue(mime, "Extension");
                info.FileExtension = fileExtension;
                using var ext2     = RegistryUtility.TryOpenSubKey(Reg.ClassesRoot, fileExtension);
                if (ext2 == null)
                {
                    return(info);
                }
                h = RegistryUtility.TryGetStringValue(ext2, null);
            }
            else
            {
                info.ContentType = RegistryUtility.TryGetStringValue(ext, "Content Type");
                h = RegistryUtility.TryGetStringValue(ext, null);
            }

            if (string.IsNullOrEmpty(h))
            {
                using var ext2 = RegistryUtility.TryOpenSubKey(ext, "OpenWithProgids");
                if (ext2 == null)
                {
                    return(info);
                }
                h = RegistryUtility.TryGetValueNames(ext2).FirstOrDefault(ele => !string.IsNullOrWhiteSpace(ele));
                if (string.IsNullOrEmpty(h))
                {
                    return(info);
                }
            }

            using var assoc = RegistryUtility.TryOpenSubKey(Reg.ClassesRoot, h);
            if (assoc == null)
            {
                return(info);
            }
            info.Name      = RegistryUtility.TryGetStringValue(assoc, null) ?? RegistryUtility.TryGetStringValue(assoc, "FriendlyTypeName");
            using var icon = RegistryUtility.TryOpenSubKey(assoc, "DefaultIcon");
            if (icon != null)
            {
                info.Icon = RegistryUtility.TryGetStringValue(icon, null);
            }
            using var shell = RegistryUtility.TryOpenSubKey(assoc, "shell");
            if (shell == null)
            {
                return(info);
            }
            var defaultCommand = RegistryUtility.TryGetStringValue(shell, null);
            var commands       = RegistryUtility.TryOpenSubKeys(shell)?.ToList();

            if (commands.Count < 1)
            {
                return(info);
            }
            foreach (var command in commands)
            {
                var name = command.Name;
                var pos  = name?.LastIndexOf('\\') ?? -1;
                if (pos >= 0)
                {
                    name = name.Substring(pos + 1);
                }
                var cmd = new FileOpenCommandInfo
                {
                    Key  = name,
                    Name = RegistryUtility.TryGetStringValue(command, null)
                };
                var exe = RegistryUtility.TryOpenSubKey(command, "command");
                if (exe == null)
                {
                    continue;
                }
                var c = RegistryUtility.TryGetStringValue(exe, null);
                if (string.IsNullOrWhiteSpace(c))
                {
                    continue;
                }
                cmd.Command = c;
                info.Commands.Add(cmd);
            }

            if (!string.IsNullOrWhiteSpace(defaultCommand))
            {
                info.DefaultCommand = info.Commands.FirstOrDefault(ele => defaultCommand.Equals(ele.Key, StringComparison.OrdinalIgnoreCase));
            }

            if (info.DefaultCommand == null)
            {
                info.DefaultCommand = info.Commands.FirstOrDefault();
            }
            return(info);
        }