In this article I will explain with an example, how to get MIME type (Content Type) of a File in ASP.Net using C# and VB.Net.
Files Folder Location
Files are stored in the Folder (Directory) named Files as shown below.
Namespaces
You need to import the following namespace.
C#
VB.Net
Getting MIME Type (Content Type) of a File using C# and VB.Net
First, the path of the file is passed as parameter to GetFileName method of Path class which gives the name of the file.
Finally, the file name is passed as parameter to GetMimeMapping method of MimeMapping class which determines the MIME type or the Content Type of the file.
C#
string word = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath"~/Files/sample.docx")));
string excel = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.xlsx")));
string pdf = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.pdf")));
string image = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.png")));
VB.Net
Dim word As String = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath"~/Files/sample.docx")))
Dim excel As String = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.xlsx")))
Dim pdf As String = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.pdf")))
Dim image As String = MimeMapping.GetMimeMapping(Path.GetFileName(Server.MapPath("~/Files/sample.png")))
Screenshot
Downloads