Feedback

Mime type of file

Sprache: C#

Returns mime type from file extension out of registry
using System.IO;
using Microsoft.Win32;
static string GetMimeType(FileInfo fileInfo)
{
    const string unkownMimeType = "application/unknown";
    var regKey = Registry.ClassesRoot.OpenSubKey(fileInfo.Extension.ToLower());

    if (regKey == null)
        return unkownMimeType;
            
    var contentType = regKey.GetValue("Content Type");

    return (contentType == null) ? unkownMimeType : contentType.ToString();
}
using System.IO;
using Microsoft.Win32;
static string GetMimeType(FileInfo fileInfo)
{
    const string unkownMimeType = "application/unknown";
    var regKey = Registry.ClassesRoot.OpenSubKey(fileInfo.Extension.ToLower());

    if (regKey == null)
        return unkownMimeType;
            
    var contentType = regKey.GetValue("Content Type");

    return (contentType == null) ? unkownMimeType : contentType.ToString();
}