Provides a huge two-way mapping of file extensions to mime types and mime types to file extensions, e.g.:
Most mime types that have multiple possible extensions are pre-defined to get the most common extension when looking
up extension by mime type. Since multiple extensions can map to the same mime type, it is not necessary that
GetExtension(GetMimeType(ext)) returns the original extension - it will return the most common extension.
Originally posted on StackOverflow here: http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension
Please submit pull requests for any additions. Thanks!
After installation MimeTypeMap, include the following using statement in your class:
Getting the mime type to an extension
Console.WriteLine("txt -> " + MimeTypeMap.GetMimeType("txt")); // "text/plain"
Pass in a string extension and get a mime type back. Optionally include the period. If not it will be added before looking up the mime type.
If no mime type is found then the generic "application/octet-stream" is returned.
Getting the extension to a mime type
Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtension("audio/wav")); // ".wav"
Pass in a mime type and get an extension back. If the mime type is not registered, an error is thrown.