/// <summary> /// 删除虚拟目录 /// </summary> /// <param name="iisVir"></param> public static void RemoveIISWebVirtualDir(IISWebVirtualDir iisVir) { DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC"); DirectoryEntry Server = returnIISWebserver(iisVir.Parent.index); if (Server == null) { throw(new Exception("找不到给定的站点!")); } IEnumerator ie = Server.Children.GetEnumerator(); ie.MoveNext(); Server = (DirectoryEntry)ie.Current; DirectoryEntry VirDir; ie = Server.Children.GetEnumerator(); while (ie.MoveNext()) { VirDir = (DirectoryEntry)ie.Current; if (VirDir.Name.ToLower() == iisVir.Name.ToLower()) { Server.Children.Remove(VirDir); Server.CommitChanges(); return; } } throw(new Exception("找不到给定的虚拟目录!")); }
/// <summary> /// 创建虚拟目录 /// </summary> /// <param name="iisVir"></param> /// <param name="deleteIfExist"></param> public static void CreateIISWebVirtualDir(IISWebVirtualDir iisVir, bool deleteIfExist) { if (iisVir.Parent == null) { throw(new Exception("IISWebVirtualDir没有所属的IISWebServer!")); } DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC"); DirectoryEntry Server = returnIISWebserver(iisVir.Parent.index); if (Server == null) { throw(new Exception("找不到给定的站点!")); } IEnumerator ie = Server.Children.GetEnumerator(); ie.MoveNext(); Server = (DirectoryEntry)ie.Current; if (deleteIfExist) { DirectoryEntry VirDir; ie = Server.Children.GetEnumerator(); while (ie.MoveNext()) { VirDir = (DirectoryEntry)ie.Current; if (VirDir.Name.ToLower().Trim() == iisVir.Name.ToLower()) { Server.Children.Remove(VirDir); Server.CommitChanges(); break; } } } try { DirectoryEntry vir; vir = Server.Children.Add(iisVir.Name, "IIsWebVirtualDir"); vir.Properties["Path"][0] = iisVir.Path; vir.Properties["DefaultDoc"][0] = iisVir.DefaultDoc; vir.Properties["EnableDefaultDoc"][0] = iisVir.EnableDefaultDoc; vir.Properties["AccessScript"][0] = iisVir.AccessScript; vir.Properties["AccessRead"][0] = iisVir.AccessRead; vir.Invoke("AppCreate", true); Server.CommitChanges(); vir.CommitChanges(); } catch (Exception es) { throw(es); } }
internal void Add_(IISWebVirtualDir WebVirtualDir) { try { this.List.Add(WebVirtualDir); } catch { throw(new Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入")); } }
/// <summary> /// /// </summary> /// <param name="WebVirtualDir"></param> public void Remove(IISWebVirtualDir WebVirtualDir) { for (int i = 0; i < this.List.Count; i++) { if ((IISWebVirtualDir)this.List[i] == WebVirtualDir) { this.List.RemoveAt(i); IISManagement.RemoveIISWebVirtualDir(WebVirtualDir); return; } } }
/// <summary> /// /// </summary> /// <param name="WebVirtualDir"></param> public void Add(IISWebVirtualDir WebVirtualDir) { WebVirtualDir.Parent = this.Parent; try { this.List.Add(WebVirtualDir); } catch { throw(new Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入")); } IISManagement.CreateIISWebVirtualDir(WebVirtualDir, true); }
private void start() { DirectoryEntry Service = new DirectoryEntry("IIS://" + Machinename + "/W3SVC"); DirectoryEntry Server; DirectoryEntry Root = null; DirectoryEntry VirDir; IEnumerator ie = Service.Children.GetEnumerator(); IEnumerator ieRoot; IISWebServer item; IISWebVirtualDir item_virdir; bool finded = false; while (ie.MoveNext()) { Server = (DirectoryEntry)ie.Current; if (Server.SchemaClassName == "IIsWebServer") { item = new IISWebServer(); item.index = Convert.ToInt16(Server.Name); item.ServerComment = (string)Server.Properties["ServerComment"][0]; item.AccessRead = (bool)Server.Properties["AccessRead"][0]; item.AccessScript = (bool)Server.Properties["AccessScript"][0]; item.DefaultDoc = (string)Server.Properties["DefaultDoc"][0]; item.EnableDefaultDoc = (bool)Server.Properties["EnableDefaultDoc"][0]; item.EnableDirBrowsing = (bool)Server.Properties["EnableDirBrowsing"][0]; ieRoot = Server.Children.GetEnumerator(); while (ieRoot.MoveNext()) { Root = (DirectoryEntry)ieRoot.Current; if (Root.SchemaClassName == "IIsWebVirtualDir") { finded = true; break; } } if (finded) { item.Path = Root.Properties["path"][0].ToString(); } if (string.IsNullOrEmpty(Convert.ToString(Server.Properties["Serverbindings"].Value))) { continue; } item.Port = Convert.ToInt16(Server.Properties["Serverbindings"][0].ToString().Split(':')[1]); this.WebServers.Add_(item); ieRoot = Root.Children.GetEnumerator(); while (ieRoot.MoveNext()) { VirDir = (DirectoryEntry)ieRoot.Current; if (VirDir.SchemaClassName != "IIsWebVirtualDir" && VirDir.SchemaClassName != "IIsWebDirectory") { continue; } item_virdir = new IISWebVirtualDir(item.ServerComment); item_virdir.Name = VirDir.Name; item_virdir.AccessRead = (bool)VirDir.Properties["AccessRead"][0]; item_virdir.AccessScript = (bool)VirDir.Properties["AccessScript"][0]; item_virdir.DefaultDoc = (string)VirDir.Properties["DefaultDoc"][0]; item_virdir.EnableDefaultDoc = (bool)VirDir.Properties["EnableDefaultDoc"][0]; if (VirDir.SchemaClassName == "IIsWebVirtualDir") { item_virdir.Path = (string)VirDir.Properties["Path"][0]; } else if (VirDir.SchemaClassName == "IIsWebDirectory") { item_virdir.Path = Root.Properties["Path"][0] + "\\" + VirDir.Name; } item.WebVirtualDirs.Add_(item_virdir); } } } }