Discovering MIME types

I've been working on a little application to scratch an itch and I ran across the need to tell whether a certain file was an audio file or not. Rather than having a list of extensions (*.mp3, *.wma, ...) I decided to see if I could dig out the MIME type that Windows tracks.

Turns out (not surprisingly) that this info is stored in the registry in the HKEY_CLASSES_ROOT under the file extension. So if you know the extension you can figure out the MIME type.

Interestingly some extensions only have the Content Type value while others have both Content Type and PerceivedType.

Here is the routine I use to determine if a file is an audio type:

public bool IsAudio(FileInfo file)
{
	using (RegistryKey k = Registry.ClassesRoot.OpenSubKey(file.Extension))
	{
		if (k != null)
		{
			string value = (string)k.GetValue("PerceivedType");
			if (value != null)
			{
				return (value == "audio");
			}

			value = (string)k.GetValue("Content Type");
			if (value != null)
			{
				return (value.Split('/')[0] == "audio");
			}
		}
		return false;
	}
}