C#完成創立,刪除,查找,設置裝備擺設虛擬目次實例詳解。本站提示廣大學習愛好者:(C#完成創立,刪除,查找,設置裝備擺設虛擬目次實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成創立,刪除,查找,設置裝備擺設虛擬目次實例詳解正文
本文實例講述了C#完成創立,刪除,查找,設置裝備擺設虛擬目次的辦法。分享給年夜家供年夜家參考。詳細以下:
#region<<虛擬目次>> /// <summary> /// 創立虛擬目次 /// </summary> /// <param >虛擬目次別號</param> /// <param >內容地點途徑</param> public static bool CreateVirtualDirectory(string webSite,string virtualName, string path) { if (virtualName.Equals(string.Empty) || (!Directory.Exists(path))) { MessageBox.Show("1.虛擬目次別號不克不及為空/r/n 2.虛擬目次內容地點途徑能夠不存在", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } try { DirectoryEntry tbEntry = FindVirtualDir(webSite,virtualName);//查找虛擬目次 if (tbEntry == null) //找不到則創立虛擬目次 { string IISpath = "IIS://localhost/W3SVC/"+webSite+"/ROOT";//IIS參數格局 System.DirectoryServices.DirectoryEntry root = new DirectoryEntry(IISpath); tbEntry = root.Children.Add(virtualName, root.SchemaClassName);//創立虛擬目次 } return UpdateVirDir(tbEntry, path, virtualName); } catch (Exception ex) { MessageBox.Show("1.請確認IIS能否曾經裝置/r/n2.虛擬目次有能夠曾經存在/r/n (Error:" + ex.ToString(), "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); WriterLog("CreateVir:/r/n" + ex.ToString()); return false; } } /// <summary> /// 獲得網站列表 /// </summary> /// <returns></returns> public static List<webSiteInfo> GetWebSiteList() { try { List<webSiteInfo> infoList = new List<webSiteInfo>(); DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC"); foreach (DirectoryEntry e in deRoot.Children) { webSiteInfo info = new webSiteInfo(); PropertyValueCollection pvc = e.Properties["ServerBindings"]; if (pvc.Count <= 0) continue; String[] srvBindings = pvc[0].ToString().Split(':'); info.IpAddress = srvBindings[0] == string.Empty ? appCode.ToolBox.GetIP() : srvBindings[0];//綁定的IP地址 info.Port = srvBindings[1];//對應端標語 info.Name = e.Name;//地點站點索引 infoList.Add(info); } return infoList; } catch (Exception ex) { WriterLog(ex.ToString()); return null; } } /// <summary> /// 刪除虛擬目次 /// </summary> /// <param ></param> /// <param ></param> /// <returns></returns> public static bool DelVirtualDirectory(string webSite,string nameDirectory) { try { DirectoryEntry deRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + webSite + "/ROOT"); try { deRoot.Children.Remove(FindVirtualDir(webSite,nameDirectory)); deRoot = null; return true; } catch { return true; } } catch (Exception e) { WriterLog(e.ToString()); return false; } } /// <summary> /// 修正虛擬目次 /// </summary> /// <param ></param> /// <param ></param> /// <param ></param> /// <returns></returns> public static bool UpdateVirDir(DirectoryEntry tbEntry, string path, string virtualName) { if (tbEntry == null) return false; tbEntry.Properties["Path"][0] = path;//物理目次 tbEntry.Invoke("AppCreate", true);//創立運用法式 tbEntry.Properties["AccessRead"][0] = true;//許可拜訪 tbEntry.Properties["ContentIndexed"][0] = true; ;// tbEntry.Properties["DefaultDoc"][0] = "index.asp,Default.aspx"; tbEntry.Properties["AppFriendlyName"][0] = virtualName;//運用法式名 tbEntry.Properties["AccessScript"][0] = true;//拜訪權限 tbEntry.Properties["DontLog"][0] = true;//日記 tbEntry.Properties["AuthFlags"][0] = 1;//驗證,0不,1許可,3根本身份 tbEntry.CommitChanges();//開端運用屬性 return true; } /// <summary> /// 查找虛擬目次 /// </summary> /// <param ></param> /// <returns></returns> public static DirectoryEntry FindVirtualDir(string webSite,string virtualName) { if (virtualName.Equals(string.Empty)) { MessageBox.Show("虛擬目次別號不克不及為空", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return null; } try { string IISpath = "IIS://localhost/W3SVC/" + webSite + "/ROOT";//IIS參數格局 System.DirectoryServices.DirectoryEntry root = new DirectoryEntry(IISpath);//實例IIS類 return root.Children.Find(virtualName, root.SchemaClassName);//查找虛擬目次) } catch (Exception ex) { WriterLog("虛擬目次不存在: " + ex.Message); return null; } } /// <summary> /// 為虛擬目次增長擴大名 /// </summary> /// <param >虛擬目次名</param> /// <param >擴大名列表</param> public static void SetMime(string webSite,string virtualName, Dictionary<string, string> exList) { if (virtualName.Equals(string.Empty)) { MessageBox.Show("虛擬目次別號不克不及為空", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DirectoryEntry tbEntry = FindVirtualDir(webSite, virtualName); if (tbEntry == null) return; System.DirectoryServices.PropertyValueCollection mime = tbEntry.Properties["MimeMap"]; foreach (string e in exList.Keys) { IISOle.MimeMapClass Ex = new IISOle.MimeMapClass(); Ex.Extension = e; Ex.MimeType = exList[e]; if (ContianValue(Ex, mime) != null) continue; mime.Add(Ex); Ex = null; } tbEntry.CommitChanges(); } /// <summary> /// 刪除擴大名 /// </summary> /// <param ></param> /// <param ></param> /// <returns></returns> public static bool DelMime(IISOle.IISMimeType type,string webSite, string virtualName) { DirectoryEntry tbEntry = FindVirtualDir(webSite, virtualName); if (tbEntry == null) return false; System.DirectoryServices.PropertyValueCollection mime = tbEntry.Properties["MimeMap"]; object v = ContianValue(type, mime); if (v != null) mime.Remove(v); tbEntry.CommitChanges(); return true; } /// <summary> /// 搜刮取指定擴大名對象 /// </summary> /// <param ></param> /// <param ></param> /// <returns></returns> private static object ContianValue(IISOle.IISMimeType type, PropertyValueCollection mime) { foreach (object v in mime) { IISOle.IISMimeType e = (IISOle.IISMimeType)v; if (type.Extension.ToLower().Equals(e.Extension.ToLower()) && (type.MimeType.ToLower().Equals(e.MimeType.ToLower()))) { e = null; return v; } } return null; } /// <summary> /// 取得虛擬目次的擴大名列表 /// </summary> /// <param ></param> /// <returns></returns> public static Dictionary<string, string> GetMimeList(string webSite,string virtualName) { if (virtualName.Equals(string.Empty)) { MessageBox.Show("虛擬目次別號不克不及為空", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning); return null; } try { Dictionary<string, string> Res = new Dictionary<string, string>(); DirectoryEntry tbEntry = FindVirtualDir(webSite,virtualName); if (tbEntry == null) return null; System.DirectoryServices.PropertyValueCollection _mime = tbEntry.Properties["MimeMap"]; foreach (object e in _mime) { IISOle.IISMimeType v = (IISOle.IISMimeType)e; if (!Res.ContainsKey(v.Extension)) Res.Add(v.Extension, v.MimeType); v = null; } return Res; } catch (Exception ex) { WriterLog("GetMimeList; " + ex.Message); return null; } } #endregion
願望本文所述對年夜家的C#法式設計有所贊助。