IIS Home @ it-notebook.org

C# - Enumerate websites

(Kristofer Gafvert, June 16, 2005)

This example code shows how to enumerate all websites in IIS using ADSI and C#. It will write out the identification ID for each website, which can be used to access other properties for each of the websites.

This code is only an example, and does not do any security checking (see link below).

using System.DirectoryServices;
using System;

public class IISAdmin
{
   public static void EnumerateWebsites()
   {
      try
      {
         DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
         
         foreach(DirectoryEntry de in w3svc.Children)
         {
            if(de.SchemaClassName == "IIsWebServer")
            {
                Console.WriteLine(de.Name);
            }         
         }
      }
      catch(Exception ex)
      {
         Console.WriteLine("Error in EnumerateWebsites: " + ex.Message);
      }
   }
   public static void Main()
   {
      EnumerateWebsites();
   }
}

Resources

Download code and executable
Internet Information Services SDK (MSDN)
Code Access Security