用编程方式给IIS里增加MIME类型

marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90">
最近在搞一个安装程序,其中有一步,是要给iis增加一个新的mime类型.log,好让用户能下载iis的日志。

单击在新窗口中打开图片,Ctrl+滚轮缩放图片

iis的所有的操作都可以用System.DirectoryServices里的功能完成。mime类型一定也可以。果然,经过一翻寻找,在msdn中找到。原文如下: http://msdn2.microsoft.com/en-us/library/ms525901.aspx

这里面关键一步是引用Active DS IIS Namespace Provider,这样你就能使用IISOle这个命名空间,和IISMimeType 这个类。

单击在新窗口中打开图片,Ctrl+滚轮缩放图片

实际代码并不复杂,全部在下面,比较简单,就不多解释。
using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
    class Program
    {
        static void Main(string[] args)
        {
            SetMimeTypeProperty("IIS://localhost/W3SVC/1/Root", ".hlp", "application/winhlp");
        }

        static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
        {
            //  metabasePath is of the form "IIS://<servername>/<path>"      
            //  for example "IIS://localhost/W3SVC/1/Root"     
            //  newExtension is of the form ".<extension>", for example, ".hlp"      
            //  newMimeType is of the form "<application>", for example, "application/winhlp"    
            Console.WriteLine("/nAdding {1}->{2} to the MimeMap property at {0}:",
                metabasePath, newExtension, newMimeType);

            try
            {
                DirectoryEntry path = new DirectoryEntry(metabasePath);
                PropertyValueCollection propValues = path.Properties["MimeMap"];
                Console.WriteLine(" Old value of MimeMap has {0} elements", propValues.Count);
                object exists = null;

                foreach (object value in propValues)
                {
                    // IISOle requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET              IISOle.IISMimeType mimetypeObj = (IISOle.IISMimeType)value;         
                    Console.WriteLine("  {0}->{1}", mimetypeObj.Extension, mimetypeObj.MimeType);
                    if (newExtension == mimetypeObj.Extension)
                        exists = value;
                }

                if (null != exists)
                {
                    propValues.Remove(exists);
                    Console.WriteLine(" Found an entry for {0}; removing it before adding the new one.",
                        newExtension);
                }

                IISOle.MimeMapClass newObj = new IISOle.MimeMapClass();
                newObj.Extension = newExtension;
                newObj.MimeType = newMimeType;
                propValues.Add(newObj);
                path.CommitChanges();
                Console.WriteLine(" Done.");
            }
            catch (Exception ex)
            {
                if ("HRESULT 0x80005006" == ex.Message)
                    Console.WriteLine(" Property MimeMap does not exist at {0}", metabasePath);
                else
                    Console.WriteLine("Failed in SetMimeTypeProperty with the following exception: /n{0}",
                        ex.Message);
            }
        }
    }
}
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值