[点晴永久免费OA]C#动态添加IIS个各个版本的MIME类型
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
主要是介绍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()); } } } 该文章在 2022/4/19 16:35:25 编辑过 |
关键字查询
相关文章
正在查询... |