查找新添加的扩展的 MIME 类型

发布于 2024-12-15 04:06:00 字数 199 浏览 0 评论 0原文

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

as shown here its possible to get the "default" IIS mime types from HKEY_Classes_Root.
However when I register a new type I cannot find the entry - does anyone know how I get read all IIS registered mime types progamatically ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2024-12-22 04:06:00

很抱歉回答我自己的问题,但答案发布在此处< /a> (如下所示)解决了 IIS 6 和 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);
                }
            }

Sorry to answer my own question but the answer posted here (shown below) resolves this for both 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);
                }
            }
剑心龙吟 2024-12-22 04:06:00

您可能需要将其添加到 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;

    }
}

我可以在 .NET 中设置 IIS MIME 类型吗?< /a>

You may have to add it in IIS.

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

Update

You could try the 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文