Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

content type by extension

Is there any built in function that returns the content type based on the file extension?

like image 259
mrblah Avatar asked Dec 15 '09 20:12

mrblah


2 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.

like image 150
CesarGon Avatar answered Sep 21 '22 20:09

CesarGon


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

like image 28
Marino van der Heijden Avatar answered Sep 17 '22 20:09

Marino van der Heijden