Source: Portal
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.DirectoryServices;usingSystem.Diagnostics;namespacewindowsformsapplication13{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } voidShowentry (DirectoryEntry entry) {foreach(DirectoryEntry Childentryinchentry. Children) {if(Childentry.schemaclassname = ="IIsWebServer") {Debug.Print (Childentry.schemaclassname+":"+ childentry.properties["ServerComment"]. Value.tostring ()); Debug.Print ("*********************start*************************"); foreach(varNameinchchildEntry.Properties.PropertyNames) {Debug.Print (Name+":"+Childentry.properties[name. ToString ()]. Value); } Debug.Print ("*********************end*************************"); } Else if(Childentry.schemaclassname = ="IIsWebVirtualDir") {Debug.Print (Childentry.schemaclassname+":"+childentry.name); Debug.Print ("*********************start*************************"); foreach(varNameinchchildEntry.Properties.PropertyNames) {Debug.Print (Name+":"+Childentry.properties[name. ToString ()]. Value); } Debug.Print ("*********************end*************************"); } Else { //Debug.Print (childentry.schemaclassname);} showentry (Childentry); } } Private voidForm1_Load (Objectsender, EventArgs e) {Showentry (NewDirectoryEntry ("Iis://localhost/w3svc")); } }}
Get the IIS Tree catalog:
Public classSiteinfo { Public stringName {Get;Set; } Public stringPath {Get;Set; } Public BOOLIsapp {Get;Set; } PublicList<siteinfo> Children {Get;Set; } } List<SiteInfo>Getsitelist (DirectoryEntry entry) {varresult =NewList<siteinfo>(); foreach(DirectoryEntry Childentryinchentry. Children) {varSites =getsitelist (childentry); if(Childentry.schemaclassname = ="IIsWebServer") { varsite =NewSiteinfo (); Site. Name= childentry.properties["ServerComment"]. Value.tostring (); Site. Path= sites[0]. Path; Site. Isapp=true; Site. Children=NewList<siteinfo>(); foreach(varSubsiteinchsites[0]. Children) site. Children.add (subsite); Result. ADD (site); } Else if(Childentry.schemaclassname = ="IIsWebVirtualDir") { varsite =NewSiteinfo (); Site. Name=Childentry.name; Site. Path= childentry.properties["Path"]. Value.tostring (); Site. Children=sites; if(ChildEntry.Properties.Contains ("AppRoot") && childentry.properties["AppRoot"]. Value! =NULL&&!string. IsNullOrEmpty (childentry.properties["AppRoot"]. Value.tostring ())) site. Isapp=true; Result. ADD (site); } } returnresult; }
C # get all IIS sites and virtual directories and applications (including name and details)