/// <summary> /// Gets a file's perceived type based on its extension. /// </summary> /// <param name="fileName">The file name. May not be null..</param> /// <returns>An instance of the PerceivedType type.</returns> 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(null, nameof(fileName)); } extension = extension.ToLowerInvariant(); if (_perceivedTypes.TryGetValue(extension, out Perceived ptype)) { return(ptype); } lock (SyncObject) { if (!_perceivedTypes.TryGetValue(extension, out ptype)) { ptype = new Perceived(); ptype.Extension = extension; using (var key = Registry.ClassesRoot.OpenSubKey(extension, writable: false)) { if (key != null) { var ct = key.GetStringValue("PerceivedType"); if (ct != null) { ptype.PerceivedType = Extensions.GetEnumValue(ct, PerceivedType.Custom); ptype.PerceivedTypeSource = PerceivedTypeSource.SoftCoded; } else { ct = key.GetStringValue("Content Type"); if (ct != null) { var pos = ct.IndexOf('/'); if (pos > 0) { ptype.PerceivedType = Extensions.GetEnumValue(ct.Substring(0, pos), PerceivedType.Custom); ptype.PerceivedTypeSource = PerceivedTypeSource.Mime; } } } } } if (ptype.PerceivedType == PerceivedType.Unknown) { var text = IntPtr.Zero; var type = PerceivedType.Unknown; var source = PerceivedTypeSource.Undefined; var hr = AssocGetPerceivedType(extension, ref type, ref source, ref text); if (hr != 0) { ptype.PerceivedType = PerceivedType.Unspecified; ptype.PerceivedTypeSource = PerceivedTypeSource.Undefined; } else { ptype.PerceivedType = type; ptype.PerceivedTypeSource = source; } } _perceivedTypes.Add(ptype.Extension, ptype); } return(ptype); } }
/// <summary> /// Gets a file's perceived type based on its extension. /// </summary> /// <param name="fileName">The file name. May not be null..</param> /// <returns>An instance of the PerceivedType type.</returns> 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.ToLowerInvariant(); 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('/'); 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); } }