c#动态添加IIS个各个版本的MIME类型

主要是介绍IIS6和IIS7.

IIS6主要是运用System.DirectoryServices.dll。IIS7用到Microsoft.Web.Administration.dll,该DLL在IIS7目录中存在,拷贝即可。


代码如下:

        /// <summary>
        /// 取得IIS版本
        /// </summary>
        /// <returns></returns>
        private int GetIISVersion()
        {
            DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
            string strVersion = getEntity.Properties["MajorIISVersionNumber"].Value.ToString();

            return Convert.ToInt32(strVersion);
        }

        主体实现方法:

       private void Main() {

        //判断IIS版本
            int iisVersion = this.GetIISVersion();

            //IIS7
            if (iisVersion > 6)
            {
                try
                {
                    ServerManager server = new ServerManager();
                    Configuration confg = server.GetWebConfiguration(webSiteName); //webSiteName站点名称

                    ConfigurationSection section;
                    section = confg.GetSection("system.webServer/staticContent");     //取得MimeMap所有节点(路径为:%windir%\Windows\System32\inetsrv\config\applicationHost.config)

                    ConfigurationElement filesElement = section.GetCollection();
                    ConfigurationElementCollection filesCollection = filesElement.GetCollection();

                    ConfigurationElement newElement = filesCollection.CreateElement(); //新建MimeMap节点

                    newElement.Attributes["fileExtension"].Value = this.txtName.Text;
                    newElement.Attributes["mimeType"].Value = this.txtMimeType.Text;

                    if (filesCollection.Contains(newElement))
                    {
                        MessageBox.Show("您添加的类型已存在,请重新添加!");
                        return;
                    }

                    filesCollection.Add(newElement);
                    server.CommitChanges();  //更改目录

                    MessageBox.Show("添加成功!");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("eror:" + ex.Message.ToString());
                }
            }
            else //IIS6
            {
                try
                {
                    int id = this.GetSelectIdByWebSiteName(webSiteName); //取得站点的标识符ID
                    DirectoryEntry path = new DirectoryEntry(string.Format("IIS://localhost/w3svc/{0}/root", id));
                    PropertyValueCollection propValues = path.Properties["MimeMap"];

                    string newExtension = this.txtName.Text;    //扩展名     
                    string newMimeType = this.txtMimeType.Text; //MIME类型

                    object exists = null;
                    foreach (object value in propValues)
                    {
                        IISOle.IISMimeType mimetypeObj = (IISOle.IISMimeType)value;

                        if (newExtension == mimetypeObj.Extension)
                        {
                            exists = value;
                        }
                    }

                    if (null != exists)
                    {
                        //propValues.Remove(exists);
                        MessageBox.Show("您添加的类型已存在,请重新添加!");
                        return;
                    }

                    IISOle.MimeMapClass _NewMime = new IISOle.MimeMapClass();  //新建MIME类型
                    _NewMime.Extension = newExtension;
                    _NewMime.MimeType = newMimeType;

                    propValues.Add(_NewMime); //添加MIME类型
                    path.CommitChanges(); //更改目录

                    MessageBox.Show("添加成功!");
                    this.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值