If you have a mime type and you want to find the default extension for it, you can get this from the Extension value in the following registry key:
HKEY_CLASSES_ROOT\MIME\Database\Content Type\<mime type>
public static string GetDefaultExtension(string mimeType) { string result; RegistryKey key; object value; key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false); value = key != null ? key.GetValue("Extension", null) : null; result = value != null ? value.ToString() : string.Empty; return result; }
One the other hand, if you have a file extension and you want to know what that mime type is, you can get that via the Content Type value of this key:
HKEY_CLASSES ROOT\<extension>
public static string GetMimeTypeFromExtension(string extension) { string result; RegistryKey key; object value; if (!extension.StartsWith(".")) extension = "." + extension; key = Registry.ClassesRoot.OpenSubKey(extension, false); value = key != null ? key.GetValue("Content Type", null) : null; result = value != null ? value.ToString() : string.Empty; return result; }
All content Copyright (c) by Cyotek Ltd or its respective writers. Permission to reproduce news and web log entries and other RSS feed content in unmodified form without notice is granted provided they are not used to endorse or promote any products or opinions (other than what was expressed by the author) and without taking them out of context. Written permission from the copyright owner must be obtained for everything else.
Original URL of this content is https://www.cyotek.com/blog/mime-types-and-file-extensions?source=rss.