Exemple #1
0
        /// <summary>
        /// Gets the path extension for a given MIME content type.
        /// </summary>
        /// <param name="contentType">The input MIME content type.</param>
        /// <param name="def">The default path extension to return if any error occurs.</param>
        /// <returns>The MIME content type's path extension.</returns>
        public static string GetExtensionForContentType(string contentType, string def)
        {
            var helper = new PermissionHelper();

            if (string.IsNullOrEmpty(contentType))
            {
                return def;
            }
            string ext = "";
            if (!helper.GetIsRegistryAvailable())
            {
                if (MimeTypes.ContainsValue(contentType))
                {
                    foreach (KeyValuePair<string, string> pair in MimeTypes)
                        if (pair.Value == contentType)
                            return pair.Value;
                }
                return def;
            }

            if (helper.GetIsRegistryAvailable())
            {
                try
                {
                    RegistryKey reg = Registry.ClassesRoot;
                    reg = reg.OpenSubKey(@"MIME\Database\Content Type\" + contentType, false);
                    if (reg != null) ext = (string)reg.GetValue("Extension", def);
                }
                catch (Exception)
                {
                    ext = def;
                }
            }
            return ext;
        }
 /// <summary>
 /// Gets the MIME content type for a given path extension.
 /// 
 /// </summary>
 /// <param name="extension">The input path extension.</param><param name="def">The default content type to return if any error occurs.</param>
 /// <returns>
 /// The path extension's MIME content type.
 /// </returns>
 public static string GetContentTypeForExtension(string extension, string def)
 {
   PermissionHelper permissionHelper = new PermissionHelper();
   if (string.IsNullOrEmpty(extension))
     return def;
   string str = "";
   if (!permissionHelper.GetIsRegistryAvailable())
     str = !HtmlWeb.MimeTypes.ContainsKey(extension) ? def : HtmlWeb.MimeTypes[extension];
   if (!permissionHelper.GetIsDnsAvailable())
   {
     try
     {
       RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension, false);
       if (registryKey != null)
         str = (string) registryKey.GetValue("", (object) def);
     }
     catch (Exception ex)
     {
       str = def;
     }
   }
   return str;
 }
Exemple #3
0
        /// <summary>
        /// Gets the MIME content type for a given path extension.
        /// </summary>
        /// <param name="extension">The input path extension.</param>
        /// <param name="def">The default content type to return if any error occurs.</param>
        /// <returns>The path extension's MIME content type.</returns>
        public static string GetContentTypeForExtension(string extension, string def)
        {
            var helper = new PermissionHelper();
            if (string.IsNullOrEmpty(extension))
            {
                return def;
            }
            string contentType = "";
            if (!helper.GetIsRegistryAvailable())
            {
                if (MimeTypes.ContainsKey(extension))
                    contentType = MimeTypes[extension];
                else
                    contentType = def;
            }

            if (!helper.GetIsDnsAvailable())
            {
                //do something.... not at full trust
                try
                {
                    RegistryKey reg = Registry.ClassesRoot;
                    reg = reg.OpenSubKey(extension, false);
                    if (reg != null) contentType = (string)reg.GetValue("", def);
                }
                catch (Exception)
                {
                    contentType = def;
                }
            }
            return contentType;
        }
 /// <summary>
 /// Gets the path extension for a given MIME content type.
 /// 
 /// </summary>
 /// <param name="contentType">The input MIME content type.</param><param name="def">The default path extension to return if any error occurs.</param>
 /// <returns>
 /// The MIME content type's path extension.
 /// </returns>
 public static string GetExtensionForContentType(string contentType, string def)
 {
   PermissionHelper permissionHelper = new PermissionHelper();
   if (string.IsNullOrEmpty(contentType))
     return def;
   string str = "";
   if (!permissionHelper.GetIsRegistryAvailable())
   {
     if (HtmlWeb.MimeTypes.ContainsValue(contentType))
     {
       foreach (KeyValuePair<string, string> keyValuePair in HtmlWeb.MimeTypes)
       {
         if (keyValuePair.Value == contentType)
           return keyValuePair.Value;
       }
     }
     return def;
   }
   if (permissionHelper.GetIsRegistryAvailable())
   {
     try
     {
       RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("MIME\\Database\\Content Type\\" + contentType, false);
       if (registryKey != null)
         str = (string) registryKey.GetValue("Extension", (object) def);
     }
     catch (Exception ex)
     {
       str = def;
     }
   }
   return str;
 }