MIME Types and File Extensions

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:

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;
}