Ejemplo n.º 1
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)
 {
   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;
 }
Ejemplo n.º 2
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)
 {
   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;
 }