【问题标题】:find mime types for newly added extensions查找新添加的扩展的 MIME 类型
【发布时间】:2011-12-27 02:20:42
【问题描述】:

here 所示,可以从 HKEY_Classes_Root 获取“默认”IIS mime 类型。 但是,当我注册一种新类型时,我找不到该条目 - 有谁知道我如何以编程方式读取所有 IIS 注册的 mime 类型?

【问题讨论】:

    标签: c# asp.net iis


    【解决方案1】:

    很抱歉回答我自己的问题,但发布的答案 here(如下所示)解决了 IIS 6 和 7 的问题

             NameValueCollection map = new NameValueCollection();
                using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
                {
                    PropertyValueCollection properties = entry.Properties["MimeMap"];
                    Type t = properties[0].GetType();
    
                    foreach (object property in properties)
                    {
                        BindingFlags f = BindingFlags.GetProperty;
                        string ext = t.InvokeMember("Extension", f, null, property, null) as String;
                        string mime = t.InvokeMember("MimeType", f, null, property, null) as String;
                        map.Add(ext, mime);
                    }
                }
    

    【讨论】:

      【解决方案2】:

      您可能需要在 IIS 中添加它。

      http://technet.microsoft.com/en-us/library/cc725608(WS.10).aspx

      更新

      您可以尝试 DirectoryServices API:

      public static string GetMimeTypeFromExtension(string extension)
      {
          using (DirectoryEntry mimeMap = 
                 new DirectoryEntry("IIS://Localhost/MimeMap"))
          {
              PropertyValueCollection propValues = mimeMap.Properties["MimeMap"];
      
              foreach (object value in propValues)
              {
                      IISOle.IISMimeType mimeType = (IISOle.IISMimeType)value;
      
                      if (extension == mimeType.Extension)
                      {
                              return mimeType.MimeType;
                      }
              }
      
              return null;
      
          }
      }
      

      Can I setup an IIS MIME type in .NET?

      【讨论】:

      • 抱歉 - 这如何帮助我找到已注册的类型?
      • 以编程方式?从 IIS 管理器?从注册表?请更具体。
      • 道歉 - 不是很清楚。需要在链接问题中以编程方式查找注册类型。
      • 什么版本的 IIS?我更新的答案有一个应该在 IIS6 中工作的示例。
      • 需要能够支持 iis6 和 7 - 我已经尝试过这种方法,但它不适用于 iis 7。注册表方法适用于两者,但不适用于任何新添加的类型
      猜你喜欢
      • 2018-01-20
      • 2011-05-14
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2023-03-08
      • 2010-10-03
      相关资源
      最近更新 更多