13

Is there a better way of setting mimetypes in C# than the one I am trying to do thanks in advance.

static String MimeType(string filePath) { String ret = null; FileInfo file = new FileInfo(filePath); if (file.Extension.ToUpper() == ".PDF") { ret = "application/pdf"; } else if (file.Extension.ToUpper() == ".JPG" || file.Extension.ToUpper() == ".JPEG") { ret = "image/jpeg"; } else if (file.Extension.ToUpper() == ".PNG") { ret = "image/png"; } else if (file.Extension.ToUpper() == ".GIF") { ret = "image/gif"; } else if (file.Extension.ToUpper() == ".TIFF" || file.Extension.ToUpper() == ".TIF") { ret = "image/tiff"; } else { ret = "image/" + file.Extension.Replace(".", ""); } return ret; } 
3
  • 3
    I still don't understand why the large collection of MIME types which MS uses in System.Web hasn't been made available my making the internal class public... everyone is coding their own method to get the mime type of a file name and only very few are actually doing it right. Commented Jun 24, 2010 at 16:05
  • 1
    possible duplicate of How do I get the MIME type of a file being requested in ASP.NET C#? Commented Jun 24, 2010 at 16:08
  • 1
    There's also [Using .NET, how can you find the mime type of a file based on the file signature not the extension. ](stackoverflow.com/questions/58510/…) Commented Jun 24, 2010 at 16:12

4 Answers 4

15

I got this from this blogpost:

private string GetMimeType (string fileName) { string mimeType = "application/unknown"; string ext = System.IO.Path.GetExtension(fileName).ToLower(); Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); if (regKey != null && regKey.GetValue("Content Type") != null) mimeType = regKey.GetValue("Content Type").ToString(); return mimeType; } 
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to suggest the registry. However if you're running in partial trust mode I think you need to explicitly grant registry access else it'll be refused. I'd probably also cache the requests in your app rather than query every time but that's more from general unease of calling out to the registry rather than hard data. Maybe also worth point out: rather than app/unknown, IIS will refuse to serve a file if there's no registry Content Type.
11

Just this one line is needed to do this.

System.Web.MimeMapping.GetMimeMapping(FileName) 

Note: It's .NET 4.5+ only

1 Comment

You might want to include Rup's comment in your answer (link to the docs and that it's 4.5+).
7

A Dictionary<String,String> may prove to be clearer.

private static Dictionary<String, String> mtypes = new Dictionary<string, string> { {".PDF", "application/pdf" }, {".JPG", "image/jpeg"}, {".PNG", "image/png"}, {".GIF", "image/gif"}, {".TIFF","image/tiff"}, {".TIF", "image/tiff"} }; static String MimeType(String filePath) { System.IO.FileInfo file = new System.IO.FileInfo(filePath); String filetype = file.Extension.ToUpper(); if(mtypes.Keys.Contains<String>(filetype)) return mtypes[filetype]; return "image/" + filetype.Replace(".", "").ToLower(); } 

Comments

4

If you don't have registry access or don't want to use the registry, you could always use a Dictionary for that.

Dictionary<string,string> mimeTypes = new Dictionary<string,string>() { { ".PDF","application/pdf"}, { ".JPG", "image/jpeg" }, { ".JPEG", "image/jpeg" } }; // and so on 

Then use:

string mimeType = mimeTypes[fileExtension]; 

In addition, you could store these mappings in an XML file and cache them with a file dependency, rather than keeping them in your code.

1 Comment

Still needs to be hardcoded though, I think the question is about using an existing API rather than having yet another implementation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.