C# - Determine MIME-type of a file

By , 12/25/2012
(0 ratings)
Determines the MIME type of a specific file.

Text/ and Image/ can mostly be processed directly, application/ needs to be handled different (e.g. rendering via printer).

This snippet returns the MIME type - if it is not avaliable, a "helper value" is given back (PerceivedType).

You have to use Microsoft.Win32 because of the access to the registry. Pay attention when using Windows 7+ or Server 2008 R2+ - because of different values while determing 64 bit file types.

Author: Jörg Meyer, translation by Michael List
using Microsoft.Win32;
public String GetMimeTypeInfo(String aFile)
        return Registry.ClassesRoot.OpenSubKey(Path.GetExtension(aFile)).GetValue("Content Type").ToString().ToLower();
        return Registry.ClassesRoot.OpenSubKey(Path.GetExtension(aFile)).GetValue("PerceivedType").ToString().ToLower();
Tagged with MimeType, file type, MIME types, MIME.



Log in, to comment!