# MIME Types and File Extensions

- Link: https://saguiitay.com/mime-types-and-file-extensions/
- Published: 2008-01-29T15:53:00+02:00
- Author: Itay

Whenever working with content of files, it is often useful to have a way to find
the MIME type based on a file extension, or the other way around – finding the file
extension from a MIME type. Below are 2 useful methods for such requirements:

    ```notranslate
    public static string GetExtensionFromMime(string mimeType)
    {
        try
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey("@Mime\Database\Content Type" + mimeType);
            if (key == null)
                return null;

            string str = key.GetValue(Extension) as string;
            if (string.IsNullOrEmpty(str))
                return string.Empty;

            return str;
        }
        catch
        {
            return string.Empty;
        }
    }

    public static string GetMimeFromExtension(string ext)
    {
        if (!ext.StartsWith("."))
            ext = "." + ext;

        RegistryKey key = Registry.ClassesRoot.OpenSubKey(ext);
        if (key == null)
            return null;
        return key.GetValue(Content Type) as string;
    }
    ```

### _Related_
