C# - IIS: List Websites

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

    public String Name

    public String PhysicalPath

    public ServerState Status

We're going to create an enum that lists possible states the websites can/might be in.

public enum ServerState
    Starting = 1,
    Started = 2,
    Stopping = 3,
    Stopped = 4,
    Pausing = 5,
    Paused = 6,
    Continuing = 7

Next we need to connect to the IIS WWW publishing service (W3SVC) using the DirectoryEntry class.

public static void Main()
	foreach (Website site in GetSites("IIS://localhost/W3SVC"))
			site.Name, " , ", 
			site.Identity, " , ", 
			site.Status, " , ", 

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

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


Once you run the snippet (if you've got the appropriate rights), you will notice something like the following window (if all goes according to plan)

Chances are (especially if you're using IIS 7) that you got an exception along the lines of "Unhandled Exception: System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)".

Which means its likely that you don't have the IIS 6 Management Compatibility installed (programs and features).

But surely there must be an IIS 7 alternative - why would we need to install compatibility tools etc for IIS 6 in order to manage IIS 7?

Microsoft did indeed provide an IIS 7 alternative, you'll need to include a reference to the Microsoft.Web.Administration assembly which is located within your inetsrv folder under system32.

Using the following snippet we're able to retrieve the websites via the ServerManager class.
ServerManager IIS = new ServerManager();

foreach (Site site in IIS.Sites)
        site.Name, " , ",
        site.Id, " , ",
        site.State, " , ",

And that's it, this should give you a basic starting point, if you're looking at other alternatives WMI seems to be another option worth looking into - which also seems to be an IIS 6.0 based solution.

Leave a Comment

January 29, 2015 by Christoff Truter

@sheoh, can you please elaborate?

find all list of sites in iis January 29, 2015 by sheoh

please show the needful code for it in c#

Needs rights October 13, 2014 by Christoff Truter

Hi Hernaldo The user/account accessing iis needs to have rights in order to manage websites.

remote don't work June 27, 2014 by hernaldo

if i use a remote access: System.DirectoryServices.DirectoryEntry iisServer; iisServer = new System.DirectoryServices.DirectoryEntry("IIS://remote-server/W3SVC/1"); System.DirectoryServices.DirectoryEntry rootFolder = iisServer.Children.Find("Root", "IIsWebVirtualDir"); //exception here its give error: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.DirectoryServices.dll Additional information: Access is denied.

Helpful February 11, 2014 by Anonymous

Nice! and thanks for sharing..

Thanks November 1, 2013 by nikhilsreeni

This Code is working, the linq statement is working for me thanks nikhilsreeni sry to tell, Security Code in this forum is very bad

July 18, 2013 by Graeme

Dankie! Hierdie het my baie gehelp

Super Post April 12, 2013 by Yasser

Awesome share dude ! Keep it coming. Thanks n regards, Yasser Shaikh

September 4, 2012 by kannan

content not working can u send me the source code for mail .I would list out all iis site

    Latest Posts

    Simple JavaScript Shop - Back to the future : Part 1

    March 20, 2015

    Getting my hands dirty with TypeScript

    February 17, 2015

    Quick and dirty look at TypeScript

    January 29, 2015

    My personal life in review

    January 21, 2015

    Quick rundown of possible new C# 6.0 features (perhaps, maybe - I think)

    January 19, 2015

    Proof of Concept - JavaScript Mediator Framework

    January 13, 2015

    JavaScript Tip - Benchmarking

    January 7, 2015

    Google's new and improved reCAPTCHA - simple PHP implementation

    January 5, 2015

    NASA missions to follow in 2015

    January 3, 2015

    Online content censorship - South Africa

    January 2, 2015

    Most Commented on Posts

    Moving items between listboxes in ASP.net/PHP example

    Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
    June 12, 2008

    Simple WYSIWYG Editor

    Creating a WYSIWYG textbox for your website is actually quite simple.
    February 1, 2007

    C# YouTube : Google API

    Post on how to integrate with YouTube using the Google Data API
    March 12, 2011

    Populate a TreeView Control C#

    Populate a TreeView control in a windows application.
    August 27, 2009

    Cross Browser Issues: Firefox Word Wrapping

    Firefox word wrapping issues
    June 9, 2008

    Angular   ASP.NET   Astronomy   C#   C++   Coding Horrors   Comedy   Cross Browser   Design Patterns   Google API   IIS   Integration   JavaScript   Love and War   Microsoft Office   Personal   PHP   South Africa   SQL   Threading   TypeScript   Visual Basic   XML