36

How can I get file extension by content type ?

Example I know that file is "text/css" so extension will be ".css".

private static string GetExtension(string contentType)
{
    string ext = ".";

    { DETERMINATION CODE IN HERE }

    return ext;
}
4
  • Write and populate Dicionary of know types? Apr 15, 2014 at 15:14
  • 1
    What's with all the down-voting? Apr 15, 2014 at 15:16
  • I think the down-voting is because the poster is asking us to write his code for him rather than posting what was tried and asking specific questions.
    – Craig W.
    Apr 15, 2014 at 15:19
  • 4
    If you don't even know where to begin it's hard to write code that you tried. This seems like a perfectly legitimate, and specific, question to me. Apr 15, 2014 at 15:20

2 Answers 2

63

[2019] .NET Core / Standard compatible portable way

While Bradley's answer continues to be perfect on regular old Windows machines running .NET Framework, Registry is Windows-specific and will fail when porting the app to a non-Windows environment.

Fortunately there is a very small NuGet library that essentially contains a hardcoded map of the official MIME types and corresponding extensions without any external dependencies here: https://github.com/samuelneff/MimeTypeMap. It is available on NuGet as MediaTypeMap. After installing the package calling it is as simple as:

MimeTypeMap.GetExtension("audio/wav")

To put it into your example you can simply:

private static string GetExtension(string contentType)
{
    return MimeTypes.MimeTypeMap.GetExtension(contentType);
}
3
  • 5
    The one line of code is that's why I love lazy developers!
    – NoWar
    Jan 9, 2020 at 8:12
  • 3
    Awesome stuff works perfectly since my webapp runs on Azure can't rely on the registry method Jul 16, 2020 at 14:13
  • It's true, this project exists. But unfortunately, it is badly maintained. The NuGet package is now over several years outdated and does not match the version of the source code...
    – ice1e0
    May 21 at 9:37
37

The "Best" solution that I know of is to query the registry. You can find example code here. http://cyotek.com/blog/mime-types-and-file-extensions

public static string? Extension(string mimeType)
{
    var key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
    var value = key?.GetValue("Extension", null);

    return value?.ToString();
}

Also please note that this solution will only be available on Windows only RegistryKey.OpenSubKey(string, bool) is only supported on: 'windows'.

5
  • 1
    using Microsoft.Win32;
    – Troy Gizzi
    Oct 10, 2015 at 20:13
  • 10
    This works if the system you are getting the extension from can open the file. For an xls, you need Excel installed etc Oct 13, 2015 at 11:14
  • Hi @mayowa ogundele, What is the alternate solution for getting file extension based on content type if system cannot open the file. We cannot use dictionary as it does not accept duplicate Keys (as content types repeat). Please suggest..Thank You!
    – Bhargav
    Nov 18, 2016 at 11:31
  • 2
    Hi Bhargav, you may want to try MediaTypeMap from Nuget. see link github.com/samuelneff/…. Hope it suffices for you Nov 19, 2016 at 17:33
  • Won't work for .net core running on different os
    – Saylent
    Jun 30 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.