December 16, 2010 by Christoff Truter C# IIS
A while ago the company I work for started
migrating some of their client websites, to a new (more robust) server.
What we needed (to save time etc.) was the ability to retrieve/perform certain IIS actions
programmatically using C#.
Now there is numerous ways to achieve this, one method we're going to look at involves ADSI
(Active Directory Service Interfaces) - you will find a few seriously handy classes under
the System.DirectoryServices namespace.
These classes provide us with a simple way to access Active Directory service providers e.g. LDAP,
NDS, WinNT and IIS.
In this post (as the title suggests) the aim is to retrieve a list of websites on an IIS server, along with their physical paths and site status etc.
The idea is to retrieve the data displayed in the following rows (see image below) - visible in our IIS Management tool:
Firstly create some class for storing the retrieved details from IIS, like this:
public class Website { public Int32 Identity { get; set; } public String Name { get; set; } public String PhysicalPath { get; set; } public ServerState Status { get; set; } }
public enum ServerState { Starting = 1, Started = 2, Stopping = 3, Stopped = 4, Pausing = 5, Paused = 6, Continuing = 7 }
public static void Main() { foreach (Website site in GetSites("IIS://localhost/W3SVC")) { Console.WriteLine(String.Concat ( site.Name, " , ", site.Identity, " , ", site.Status, " , ", site.PhysicalPath )); } } static IEnumerable<Website> GetSites(String Path) { DirectoryEntry IIsEntities = new DirectoryEntry(Path); foreach (DirectoryEntry IIsEntity in IIsEntities.Children) { if (IIsEntity.SchemaClassName == "IIsWebServer") { yield return new Website ( Convert.ToInt32(IIsEntity.Name), IIsEntity.Properties["ServerComment"].Value.ToString(), GetPath(IIsEntity), (ServerState)IIsEntity.Properties["ServerState"].Value ); } } } static String GetPath(DirectoryEntry IIsWebServer) { foreach (DirectoryEntry IIsEntity in IIsWebServer.Children) { if (IIsEntity.SchemaClassName == "IIsWebVirtualDir") return IIsEntity.Properties["Path"].Value.ToString(); } return null; } // Alternatively we can rewrite the preceding snippet using a linq statement /*static IEnumerable<Website> GetSites(String Path) { DirectoryEntry IIsEntities = new DirectoryEntry(Path); return (from s in IIsEntities.Children.OfType<DirectoryEntry>() where s.SchemaClassName == "IIsWebServer" select new Website { Identity = Convert.ToInt32(s.Name), Name = s.Properties["ServerComment"].Value.ToString(), PhysicalPath = (from p in s.Children.OfType<DirectoryEntry>() where p.SchemaClassName == "IIsWebVirtualDir" select p.Properties["Path"].Value.ToString()).Single(), Status = (ServerState)s.Properties["ServerState"].Value }); }*/
ServerManager IIS = new ServerManager(); foreach (Site site in IIS.Sites) { Console.WriteLine(String.Concat ( site.Name, " , ", site.Id, " , ", site.State, " , ", site.Applications["/"].VirtualDirectories[0].PhysicalPath )); }
websites May 2, 2016 by Naveen
Thanks! It helped me