예제 #1
0
        public static Perceived GetPerceivedType(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            var extension = Path.GetExtension(fileName);

            if (extension == null)
            {
                throw new ArgumentException("The extension cannot be determined from the file name", nameof(fileName));
            }

            extension = extension.ToUpperInvariant();
            if (s_perceivedTypes.TryGetValue(extension, out var ptype))
            {
                return(ptype);
            }

            if (!IsSupportedPlatform())
            {
                throw new PlatformNotSupportedException("PerceivedType is only supported on Windows");
            }

            lock (SyncObject)
            {
                var type   = PerceivedType.Unknown;
                var source = PerceivedTypeSource.Undefined;
                if (!s_perceivedTypes.TryGetValue(extension, out ptype))
                {
                    using (var key = Registry.ClassesRoot.OpenSubKey(extension, writable: false))
                    {
                        if (key != null)
                        {
                            var ct = key.GetStringValue("PerceivedType");
                            if (ct != null)
                            {
                                type   = Extensions.GetEnumValue(ct, PerceivedType.Custom);
                                source = PerceivedTypeSource.SoftCoded;
                            }
                            else
                            {
                                ct = key.GetStringValue("Content Type");
                                if (ct != null)
                                {
                                    var pos = ct.IndexOf('/', StringComparison.Ordinal);
                                    if (pos > 0)
                                    {
                                        type   = Extensions.GetEnumValue(ct.Substring(0, pos), PerceivedType.Custom);
                                        source = PerceivedTypeSource.Mime;
                                    }
                                }
                            }
                        }
                    }

                    if (type == PerceivedType.Unknown)
                    {
                        var text = IntPtr.Zero;
                        type   = PerceivedType.Unknown;
                        source = PerceivedTypeSource.Undefined;
                        var hr = AssocGetPerceivedType(extension, ref type, ref source, ref text);
                        if (hr != 0)
                        {
                            type   = PerceivedType.Unspecified;
                            source = PerceivedTypeSource.Undefined;
                        }
                    }

                    ptype = new Perceived(extension, type, source);
                    s_perceivedTypes.Add(extension, ptype);
                }

                return(ptype);
            }
        }