usingSystem;usingSystem.DirectoryServices;usingSystem.Collections.Generic;usingSystem.Text;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {DirectoryEntry Rootentry=NewDirectoryEntry ("Iis://localhost/w3svc"); intSiteID =1; foreach(DirectoryEntry entryinchRootentry.children) {if(Entry. Schemaclassname.equals ("IIsWebServer", StringComparison.OrdinalIgnoreCase)) {Console.WriteLine ("Name: {0}", entry. Name); Console.WriteLine ("Path: {0}", Iisworker.getwebsitephysicalpath (entry)); Console.WriteLine ("serverbindings: {0}", entry. properties["ServerBindings"]. Value); Console.WriteLine (); DirectoryEntry Virentry=NewDirectoryEntry (entry. Path +"/root"); foreach(DirectoryEntry EntryvirtualinchVirentry.children) {if(EntryVirtual.SchemaClassName.Equals ("IIsWebVirtualDir", StringComparison.OrdinalIgnoreCase)) {Console.WriteLine ("schemaClassName: {0}", Entryvirtual.schemaclassname); Console.WriteLine ("Name: {0}", Entryvirtual.name); Console.WriteLine ("Path: {0}", entryvirtual.properties["Path"]. Value); Console.WriteLine (); } } intID =Convert.ToInt32 (entry. Name); if(ID >=SiteID) {SiteID= ID +1; } } } } } Public classIisworker {/// <summary> ///get the physical path to the site/// </summary> /// <param name= "Rootentry" >Site Node</param> /// <returns></returns> Public Static stringGetwebsitephysicalpath (DirectoryEntry rootentry) {stringPhysicalPath =""; foreach(DirectoryEntry ChildentryinchRootentry.children) {if(Childentry.schemaclassname = ="IIsWebVirtualDir") && (childEntry.Name.ToLower () = ="Root")) { if(childentry.properties["Path"]. Value! =NULL) {PhysicalPath= childentry.properties["Path"]. Value.tostring (); } Else{PhysicalPath=""; } } } returnPhysicalPath; } }}
C # Get IIS site and virtual directory information