Is there any built in function that returns the content type based on the file extension?
-
4The extension doesn't tell you what the content type is. Don't rely on it. – Anon. Dec 15 '09 at 20:26
-
1As a follow up to Anon's comment: the extension is completely transient so an executable (.exe) could easily be renamed to one of the types your application expects and your application wouldn't be able to determine that the renamed file is actually some other type. – Nathan Taylor Dec 15 '09 at 23:48
6 Answers
Not that I know of. But you can use this code:
using Microsoft.Win32;
RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
string contentType = key.GetValue("Content Type").ToString();
You'll need to add extra code for error handling.
Note: The extension needs to be prefixed by a dot, like in .txt
.

- 15,099
- 6
- 57
- 85
-
2Is there a way to go the other direction? I have content types and I'm wanting to cache the content with the appropriate extension. – Jordan Oct 24 '11 at 14:06
-
2@Jordan: I suggest you create a question with this. People will answer there. – CesarGon Oct 25 '11 at 18:17
-
@cesargon Is it possible to let the browser download any file whose name and extension is passed through querystring. Please see my question , if you can answer this. http://stackoverflow.com/questions/9927339 – Imran Rizvi Mar 30 '12 at 09:31
Since .Net Framework 4.5 there is a class System.Web.MimeMapping
which has a complete library of mime types with methods to get the requested mime type.
See: http://msdn.microsoft.com/en-us/library/system.web.mimemapping(v=vs.110).aspx
or for the implementation of GetMimeMapping
: https://referencesource.microsoft.com/#System.Web/MimeMapping.cs

- 7,598
- 2
- 40
- 57

- 528
- 4
- 10
FYKI, Check the registry under \HKEY_CLASSES_ROOT\MIME\Database\Content Type. There will be list of content type and file extension. If you could load this information through windows API then you can get your file extension to content type mapping.
hth
UPDATE : [source][1]
public string GetMIMEType(string filepath)
{
FileInfo fileInfo = new FileInfo(filepath);
string fileExtension = fileInfo.Extension.ToLower();
// direct mapping which is fast and ensures these extensions are found
switch (fileExtension)
{
case "htm":
case "html":
return "text/html";
case "js":
return "text/javascript"; // registry may return "application/x-javascript"
}
// see if we can find extension info anywhere in the registry
//Note : there is not a ContentType key under ALL the file types , check Run --> regedit , then extensions !!!
RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, @"\\HKEY_CLASSES_ROOT");
// looks for extension with a content type
RegistryKey rkContentTypes = Registry.ClassesRoot.OpenSubKey(fileExtension);
if (rkContentTypes != null)
{
object key = rkContentTypes.GetValue("Content Type");
if (key != null)
return key.ToString().ToLower();
}
// looks for a content type with extension
// Note : This would be problem if multiple extensions associate with one content type.
RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");
foreach (string keyname in typeKey.GetSubKeyNames())
{
RegistryKey curKey = typeKey.OpenSubKey(keyname);
if (curKey != null)
{
object extension = curKey.GetValue("Extension");
if (extension != null)
{
if (extension.ToString().ToLower() == fileExtension)
{
return keyname;
}
}
}
}
return null;
}
[1]: http://www.codeproject.com/KB/dotnet/ContentType.aspx?msg=2903389#xx2903389xxenter code here

- 4,149
- 7
- 42
- 62
-
I think mrblah wants to find the content type from the extension, not the other way around. Looking into the MIME database reg key would be useful to find the file extensions for a given content type. – CesarGon Dec 15 '09 at 22:02
-
yep, you are correct, I think, your method will look into \HKEY_CLASSES_ROOT. It has extension to content type mapping. using my approach you can get the value but it would be problem if multiple extensions associate with one content type. – nayakam Dec 15 '09 at 23:17
-
Is it possible to let the browser download any file whose name and extension is passed through querystring. Please see my question , if you can answer this. http://stackoverflow.com/questions/9927339 – Imran Rizvi Mar 30 '12 at 09:30
For a more or less complete list check the map below (C++, yet it is straight forward to change it to C#).
This is how Google/Chrome recognizes content type form file extention (don't know how email body is generated - by JavaScript on the client side, or from POST on the server side - my guess is the latter). I got this list using the following trick:
Searched for most commonly used file extentions on the internet. Was lucky to find an html table so that I could copy-paste it to Excel to get a clean list of just the extentions.
Created a small file test with a few characters of contents.
Used the list of extentions in (2) and some command line magic to create files test.TXT, test.HTM, test.TIFF and so on. Linux variant is
for file in test.TXT test.HTM test.TIFF ... ; do cp test "$file"; done;
Sent all these files to my gmail as attachments. Note that some files like .exe and .zip were filtered out for security reasons. They are missing in the code below!
In gmail, downloaded the original, there one can see
Content-Type: application/pdf; name="example.pdf"
lines for each of the attached files. Parsed that in VIM to get the list below.
If you want to add an extention that is not in the list below - create small file sample.xyz, send it to yourself and look at the raw email for Content-Type. That's the trick. Hope it helps!
P.S. I know it’s not the best list but it was sufficient for me to learn how to programmatically send email attachments and choose Content-Type by file extension as good as gmail would do. Don’t be harsh if some extension types are “missing”.
static const map<string, string> ContentTypes = {
{ "TXT","text/plain" },
{ "HTM","text/html" },
{ "TIFF","image/tiff" },
{ "TMP","application/octet-stream" },
{ "TOAST","application/octet-stream" },
{ "TORRENT","application/x-bittorrent" },
{ "TTF","application/x-font-ttf" },
{ "UUE","application/octet-stream" },
{ "VCD","application/x-cdlink" },
{ "VCF","text/x-vcard" },
{ "VCXPROJ","application/xml" },
{ "VOB","application/octet-stream" },
{ "WAV","audio/x-wav" },
{ "WMA","audio/x-ms-wma" },
{ "WMV","video/x-ms-wmv" },
{ "WPD","application/wordperfect" },
{ "WPS","application/octet-stream" },
{ "XCODEPROJ","application/octet-stream" },
{ "XHTML","application/xhtml+xml" },
{ "XLR","application/octet-stream" },
{ "XLS","application/vnd.ms-excel" },
{ "XLSX","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
{ "XML","text/xml" },
{ "YUV","application/octet-stream" },
{ "ZIPX","application/octet-stream" },
{ "3DM","application/octet-stream" },
{ "3DS","application/octet-stream" },
{ "3G2","video/3gpp2" },
{ "3GP","video/3gpp" },
{ "ACCDB","application/octet-stream" },
{ "AI","application/illustrator" },
{ "AIF","audio/x-aiff" },
{ "APK","application/vnd.android.package-archive" },
{ "APP","application/octet-stream" },
{ "ASF","video/x-ms-asf" },
{ "ASP","application/octet-stream" },
{ "ASPX","application/xml" },
{ "AVI","video/x-msvideo" },
{ "BAK","application/octet-stream" },
{ "BIN","application/octet-stream" },
{ "BMP","image/bmp" },
{ "C","text/x-csrc" },
{ "CAB","application/octet-stream" },
{ "CBR","application/octet-stream" },
{ "CER","application/x-x509-ca-cert" },
{ "CFG","application/octet-stream" },
{ "CFM","application/octet-stream" },
{ "CGI","application/octet-stream" },
{ "CLASS","application/octet-stream" },
{ "CPP","text/x-c++src" },
{ "CRDOWNLOAD","application/octet-stream" },
{ "CRX","application/x-chrome-extension" },
{ "CS","text/plain" },
{ "CSR","application/octet-stream" },
{ "CSS","text/css" },
{ "CSV","text/csv" },
{ "CUE","application/octet-stream" },
{ "CUR","application/octet-stream" },
{ "DAT","application/octet-stream" },
{ "DB","application/octet-stream" },
{ "DBF","application/octet-stream" },
{ "DDS","image/vnd.ms-dds" },
{ "DEB","application/x-debian-package" },
{ "DEM","application/octet-stream" },
{ "DESKTHEMEPACK","application/octet-stream" },
{ "DLL","application/octet-stream" },
{ "DMG","application/octet-stream" },
{ "DMP","application/octet-stream" },
{ "DOC","application/msword" },
{ "DOCX","application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ "DRV","application/octet-stream" },
{ "DTD","application/xml-dtd" },
{ "DWG","application/octet-stream" },
{ "DXF","application/dxf" },
{ "EPS","application/postscript" },
{ "FLA","application/octet-stream" },
{ "FLV","video/x-flv" },
{ "FNT","application/octet-stream+fnt" },
{ "FON","application/octet-stream+fon" },
{ "GADGET","application/octet-stream" },
{ "GAM","application/octet-stream" },
{ "GED","application/octet-stream" },
{ "GIF","image/gif" },
{ "GPX","application/gpx+xml" },
{ "GZ","application/x-gzip" },
{ "H","text/x-chdr" },
{ "HQX","application/mac-binhex40" },
{ "HTML","text/html" },
{ "ICNS","application/octet-stream" },
{ "ICO","image/x-icon" },
{ "ICS","text/calendar" },
{ "IFF","application/octet-stream" },
{ "INDD","application/octet-stream" },
{ "INI","application/octet-stream" },
{ "ISO","application/octet-stream" },
{ "JAVA","application/octet-stream" },
{ "JPG","image/jpeg" },
{ "JSP","application/octet-stream" },
{ "KEY","application/octet-stream" },
{ "KEYCHAIN","application/octet-stream" },
{ "KML","application/vnd.google-earth.kml+xml" },
{ "KMZ","application/vnd.google-earth.kmz" },
{ "LOG","application/octet-stream" },
{ "LUA","application/octet-stream" },
{ "M","application/octet-stream" },
{ "M3U","audio/x-mpegurl" },
{ "M4A","audio/mp4" },
{ "M4V","video/x-m4v" },
{ "MAX","application/octet-stream" },
{ "MDB","application/octet-stream" },
{ "MDF","application/octet-stream" },
{ "MID","audio/midi" },
{ "MIM","application/octet-stream" },
{ "MOV","video/quicktime" },
{ "MP3","audio/mpeg" },
{ "MP4","video/mp4" },
{ "MPA","audio/mpeg" },
{ "MPG","video/mpeg" },
{ "MSG","application/octet-stream" },
{ "NES","application/octet-stream" },
{ "OBJ","application/octet-stream" },
{ "ODT","application/vnd.oasis.opendocument.text" },
{ "OTF","application/vnd.oasis.opendocument.formula-template" },
{ "PAGES","application/x-iwork-pages-sffpages" },
{ "PART","application/octet-stream" },
{ "PCT","application/octet-stream" },
{ "PDB","chemical/x-pdb" },
{ "PDF","application/pdf" },
{ "PHP","application/x-httpd-php" },
{ "PKG","application/octet-stream" },
{ "PL","application/octet-stream" },
{ "PLUGIN","application/octet-stream" },
{ "PNG","image/png" },
{ "PPS","application/vnd.ms-powerpoint" },
{ "PPT","application/vnd.ms-powerpoint" },
{ "PPTX","application/vnd.openxmlformats-officedocument.presentationml.presentation" },
{ "PRF","application/pics-rules" },
{ "PS","application/postscript" },
{ "PSD","application/photoshop" },
{ "PSPIMAGE","application/octet-stream" },
{ "PY","application/octet-stream" },
{ "RM","audio/x-pn-realaudio" },
{ "ROM","application/octet-stream" },
{ "RPM","application/x-rpm" },
{ "RSS","application/octet-stream" },
{ "RTF","application/rtf" },
{ "SAV","application/octet-stream" },
{ "SDF","application/octet-stream" },
{ "SH","application/x-sh" },
{ "SITX","application/octet-stream" },
{ "SLN","text/plain" },
{ "SQL","application/octet-stream" },
{ "SRT","application/octet-stream" },
{ "SVG","image/svg+xml" },
{ "SWF","application/x-shockwave-flash" },
{ "SWIFT","application/octet-stream" },
{ "TAX2016","application/octet-stream" },
{ "TEX","application/x-tex" }
};

- 393
- 3
- 9
If it's an uploaded file. You can store the contenttype in a column of the database for later use when you upload the file. There is a property named ContentType for HttpPostedFile class.

- 1,194
- 12
- 18
Here is one I wrote:
public string GetContentTypeByExtension(string strExtension)
{
switch (strExtension)
{
case ".fif":
return "application/fractals";
break;
case ".hta":
return "application/hta";
break;
case ".hqx":
return "application/mac-binhex40";
break;
case ".vsi":
return "application/ms-vsi";
break;
case ".p10":
return "application/pkcs10";
break;
case ".p7m":
return "application/pkcs7-mime";
break;
case ".p7s":
return "application/pkcs7-signature";
break;
case ".cer":
return "application/pkix-cert";
break;
case ".crl":
return "application/pkix-crl";
break;
case ".ps":
return "application/postscript";
break;
case ".setpay":
return "application/set-payment-initiation";
break;
case ".setreg":
return "application/set-registration-initiation";
break;
case ".sst":
return "application/vnd.ms-pki.certstore";
break;
case ".pko":
return "application/vnd.ms-pki.pko";
break;
case ".cat":
return "application/vnd.ms-pki.seccat";
break;
case ".stl":
return "application/vnd.ms-pki.stl";
break;
case ".wpl":
return "application/vnd.ms-wpl";
break;
case ".xps":
return "application/vnd.ms-xpsdocument";
break;
case ".z":
return "application/x-compress";
break;
case ".tgz":
return "application/x-compressed";
break;
case ".gz":
return "application/x-gzip";
break;
case ".ins":
return "application/x-internet-signup";
break;
case ".iii":
return "application/x-iphone";
break;
case ".jtx":
return "application/x-jtx+xps";
break;
case ".latex":
return "application/x-latex";
break;
case ".nix":
return "application/x-mix-transfer";
break;
case ".asx":
return "application/x-mplayer2";
break;
case ".application":
return "application/x-ms-application";
break;
case ".wmd":
return "application/x-ms-wmd";
break;
case ".wmz":
return "application/x-ms-wmz";
break;
case ".xbap":
return "application/x-ms-xbap";
break;
case ".p12":
return "application/x-pkcs12";
break;
case ".p7b":
return "application/x-pkcs7-certificates";
break;
case ".p7r":
return "application/x-pkcs7-certreqresp";
break;
case ".sit":
return "application/x-stuffit";
break;
case ".tar":
return "application/x-tar";
break;
case ".man":
return "application/x-troff-man";
break;
case ".cer":
return "application/x-x509-ca-cert";
break;
case ".zip":
return "application/x-zip-compressed";
break;
case ".xaml":
return "application/xaml+xml";
break;
case ".xml":
return "application/xml";
break;
case ".aiff":
return "audio/aiff";
break;
case ".au":
return "audio/basic";
break;
case ".mid":
return "audio/mid";
break;
case ".mid":
return "audio/midi";
break;
case ".mp3":
return "audio/mp3";
break;
case ".mp3":
return "audio/mpeg";
break;
case ".m3u":
return "audio/mpegurl";
break;
case ".mp3":
return "audio/mpg";
break;
case ".wav":
return "audio/wav";
break;
case ".aiff":
return "audio/x-aiff";
break;
case ".mid":
return "audio/x-mid";
break;
case ".mid":
return "audio/x-midi";
break;
case ".mp3":
return "audio/x-mp3";
break;
case ".mp3":
return "audio/x-mpeg";
break;
case ".m3u":
return "audio/x-mpegurl";
break;
case ".mp3":
return "audio/x-mpg";
break;
case ".wax":
return "audio/x-ms-wax";
break;
case ".wma":
return "audio/x-ms-wma";
break;
case ".wav":
return "audio/x-wav";
break;
case ".bmp":
return "image/bmp";
break;
case ".gif":
return "image/gif";
break;
case ".jpg":
return "image/jpeg";
break;
case ".jpg":
return "image/pjpeg";
break;
case ".png":
return "image/png";
break;
case ".tiff":
return "image/tiff";
break;
case ".ico":
return "image/x-icon";
break;
case ".png":
return "image/x-png";
break;
case ".mid":
return "midi/mid";
break;
case ".dwfx":
return "model/vnd.dwfx+xps";
break;
case ".css":
return "text/css";
break;
case ".323":
return "text/h323";
break;
case ".htm":
return "text/html";
break;
case ".uls":
return "text/iuls";
break;
case ".txt":
return "text/plain";
break;
case ".wsc":
return "text/scriptlet";
break;
case ".htt":
return "text/webviewhtml";
break;
case ".htc":
return "text/x-component";
break;
case ".vcf":
return "text/x-vcard";
break;
case ".xml":
return "text/xml";
break;
case ".avi":
return "video/avi";
break;
case ".mpeg":
return "video/mpeg";
break;
case ".mpeg":
return "video/mpg";
break;
case ".avi":
return "video/msvideo";
break;
case ".mpeg":
return "video/x-mpeg";
break;
case ".mpeg":
return "video/x-mpeg2a";
break;
case ".asx":
return "video/x-ms-asf";
break;
case ".asx":
return "video/x-ms-asf-plugin";
break;
case ".wm":
return "video/x-ms-wm";
break;
case ".wmv":
return "video/x-ms-wmv";
break;
case ".wmx":
return "video/x-ms-wmx";
break;
case ".wvx":
return "video/x-ms-wvx";
break;
case ".avi":
return "video/x-msvideo";
break;
}
}

- 101,809
- 122
- 424
- 632

- 11
- 1