views:

342

answers:

2

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

+1  A: 

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.

CesarGon
A: 

FYKI, Check the registry under \HKEY_CLASSES_ROOT\MIME\Database\Content Type. There will be list of content type and file extension. If you could load this information through windows API then you can get your file extension to content type mapping.

hth

UPDATE : [source][1]

public string GetMIMEType(string filepath)
    {
        FileInfo fileInfo = new FileInfo(filepath);
        string fileExtension = fileInfo.Extension.ToLower();

        // direct mapping which is fast and ensures these extensions are found
        switch (fileExtension)
        {
            case "htm":
            case "html":
                return "text/html";
            case "js":
                return "text/javascript"; // registry may return "application/x-javascript"
        }



            // see if we can find extension info anywhere in the registry
    //Note : there is not a ContentType key under ALL the file types , check Run --> regedit , then extensions !!!

        RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, @"\\HKEY_CLASSES_ROOT");

        // looks for extension with a content type
        RegistryKey rkContentTypes = Registry.ClassesRoot.OpenSubKey(fileExtension);
        if (rkContentTypes != null)
        {
            object key = rkContentTypes.GetValue("Content Type");
            if (key != null)
                return key.ToString().ToLower();
        }


        // looks for a content type with extension
  // Note : This would be problem if  multiple extensions associate with one content type.
        RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");

        foreach (string keyname in typeKey.GetSubKeyNames())
        {
            RegistryKey curKey = typeKey.OpenSubKey(keyname);
            if (curKey != null)
            {
                object extension = curKey.GetValue("Extension");
                if (extension != null)
                {
                    if (extension.ToString().ToLower() == fileExtension)
                    {
                        return keyname;
                    }
                }
            }
        }

        return null;
    }

[1]: http://www.codeproject.com/KB/dotnet/ContentType.aspx?msg=2903389#xx2903389xxenter code here

Thillakan
I think mrblah wants to find the content type from the extension, not the other way around. Looking into the MIME database reg key would be useful to find the file extensions for a given content type.
CesarGon
yep, you are correct, I think, your method will look into \HKEY_CLASSES_ROOT. It has extension to content type mapping. using my approach you can get the value but it would be problem if multiple extensions associate with one content type.
Thillakan