[點(diǎn)晴永久免費(fèi)OA]C#動(dòng)態(tài)添加IIS個(gè)各個(gè)版本的MIME類型
主要是介紹IIS6和IIS7。IIS6主要是運(yùn)用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); } 主體實(shí)現(xiàn)方法: private void Main() { //判斷IIS版本 int iisVersion = this.GetIISVersion();
//IIS7 if (iisVersion > 6) { try { ServerManager server = new ServerManager(); Configuration confg = server.GetWebConfiguration(webSiteName); //webSiteName站點(diǎn)名稱
ConfigurationSection section; section = confg.GetSection("system.webServer/staticContent"); //取得MimeMap所有節(jié)點(diǎn)(路徑為:%windir%\Windows\System32\inetsrv\config\applicationHost.config)
ConfigurationElement filesElement = section.GetCollection(); ConfigurationElementCollection filesCollection = filesElement.GetCollection();
ConfigurationElement newElement = filesCollection.createElement(); //新建MimeMap節(jié)點(diǎn)
newElement.Attributes["fileExtension"].Value = this.txtName.Text; newElement.Attributes["mimeType"].Value = this.txtMimeType.Text;
if (filesCollection.Contains(newElement)) { MessageBox.Show("您添加的類型已存在,請(qǐng)重新添加!"); 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); //取得站點(diǎn)的標(biāo)識(shí)符ID DirectoryEntry path = new DirectoryEntry(string.Format("IIS://localhost/w3svc/{0}/root", id)); PropertyValueCollection propValues = path.Properties["MimeMap"];
string newExtension = this.txtName.Text; //擴(kuò)展名 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("您添加的類型已存在,請(qǐng)重新添加!"); 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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |