找到新添加的扩展名的mime类型

时间:2011-11-11 14:54:47

标签: c# asp.net iis

如图所示here可以从HKEY_Classes_Root获取“默认”IIS mime类型。 但是,当我注册一个新类型时,我找不到该条目 - 是否有人知道如何通过progamatically读取所有IIS注册的mime类型?

2 个答案:

答案 0 :(得分: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);
                }
            }

答案 1 :(得分:0)

您可能需要在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?