Example #1
0
        public static string GetMimeTypeWithExtension(string Extension)
        {
            if (!string.IsNullOrWhiteSpace(Extension))
            {
                if (!Extension.StartsWith("."))
                {
                    Extension = "." + Extension;
                }

                string result = null;

                if (ExtensionToMimeType.TryGetValue(Extension, out result))
                {
                    return(result);
                }

                Microsoft.Win32.RegistryKey regKey = default(Microsoft.Win32.RegistryKey);
                object value = null;

                regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Extension, false);
                value  = regKey != null?regKey.GetValue("Content Type", null) : null;

                result = value != null?value.ToString() : string.Empty;

                ExtensionToMimeType[Extension] = result;
                return(result);
            }
            return(null);
        }
Example #2
0
        public string ConvertExtensionToMimeType(string extension)
        {
            if (string.IsNullOrWhiteSpace(extension))
            {
                return("application/octet-stream");
            }
            if (!extension.StartsWith("."))
            {
                extension = "." + extension;
            }
            string strExtension;

            if (ExtensionToMimeType.TryGetValue(extension, out strExtension))
            {
                return(strExtension);
            }
            switch (extension)
            {
            case ".html":
            case ".htm":
                strExtension = "text/html; charset=UTF-8";
                break;

            case ".css":
                strExtension = "text/css; charset=UTF-8";
                break;

            case ".js":
                strExtension = "application/javascript; charset=UTF-8";
                break;

            case ".json":
                strExtension = "application/json; charset=UTF-8";
                break;

            case ".map":
            case ".txt":
                strExtension = "text/plain; charset=UTF-8";
                break;

            case ".xml":
                strExtension = "text/xml; charset=UTF-8";
                break;

            case ".jpeg":
            case ".jpg":
                strExtension = "image/jpeg";
                break;

            case ".png":
                strExtension = "image/png";
                break;

            case ".gif":
                strExtension = "image/gif";
                break;

            case ".ico":
                strExtension = "image/x-icon";
                break;

            default:
                using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension, false))
                {
                    object obj = registryKey != null?registryKey.GetValue("Content Type", (object)null) : (object)null;

                    strExtension = obj != null?obj.ToString() : "application/octet-stream";

                    break;
                }
            }
            ExtensionToMimeType.TryAdd(extension, strExtension);
            return(strExtension);
        }