Monday, April 28, 2008

C#: Get MimeType from a File Name

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;
}

18 comments:

  1. KC, this is brilliant! Thank you for this solution. I've been digging around for something like this, but available information has been sparse. I found another solution, but it was very long and convoluted, and written in VB. This solved my problem perfectly!

    ReplyDelete
  2. Nice job, but it's possible not to use the Windows Register?? I ask this because it's possible to publish the ASP.NET pages on non-Windows OS.

    ReplyDelete
  3. very, very, veryvery nice. smart, simple, straightforward. perfect! thx for ending my seemingly endless search ;o)

    ReplyDelete
  4. I am glad I could help some of you. And, thanks for your kind comments.

    To answer José's question: Basically we need some kind of table or data store that would have the translation between the file name extension and it's mime type. Fortunately, Windows Registry has that info, and the post above shows how that could be done. In non-windows systems, you need to find a similar translation table, and then you could use a dataset and search on it. But unfortunately I am not the right person in non-Windows domain. Sorry, I wish I could have a better answer for you.

    ReplyDelete
  5. For non-Windows use, there is the MimeTypes.xml solution (often used in Java or PHP). Several .NET classes are already available on the www.

    The Windows Registry solution is quite simple and avoid extra work to keep the translation table up to date and coherent. Unfortunatly, using the registry also means the file extensions have to be known by Windows, which often imply some client applications to be installed on the server (it probably works if those applications have been previously installed, then removed)

    ReplyDelete
  6. This solution has a problem that i is limited to the softwares you have installed on the machine. For example, if PDF is not installed, then you cannot get the MIME type for ".pdf" using this technique.

    ReplyDelete
  7. Hi, is there any way to find the mime type without the file name, I mean my application receive a file without extension y will appreciate your help thank you.

    ReplyDelete
  8. SO BRILLIANT!
    Thanks, It's perfect solution what I looking for!
    --
    Anon, just try regist .pdf type @ HKCR/.pdf
    ---------------------------------
    (default) = AcroExch.Document
    Content Type = application/pdf
    ---------------------------------
    and do every ext/type you needs.
    :)

    ReplyDelete
  9. this is reallly nice

    thanks a lot

    bye

    ReplyDelete
  10. Excellent solution! very elegant! For all those who want to include an if and else... copy the table on http://technet.microsoft.com/en-gb/library/bb742440.aspx save as xml or in database and query there as well.

    ReplyDelete
  11. TM Technology ServicesFebruary 18, 2010 at 7:04 PM

    You da man!

    ReplyDelete
  12. This is a real life saver, as flash has the unfortunate side effect of setting all of my uploaded files to "application/octet-stream". Am I right in thinking this relies on the server having the correct applications installed to recognise the mime types?

    ReplyDelete
  13. Exactly what I needed. Excellent work... Thanks!

    ReplyDelete
  14. Pretty clean...nice. I wonder if there is a web service or something that may be more reliable?

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.