IIS Home @ it-notebook.org

C# - Find website ID by name

(Kristofer Gafvert, June 9, 2005)

This example code for IIS 5.0, IIS 5.1 and IIS 6.0 shows how to find the website ID when you know the name (description) of the website.

    using System.DirectoryServices;
    using System;
    
    public class IISAdmin
    {
       public static void GetWebsiteID(string websiteName)
       {
          DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
          
         foreach(DirectoryEntry de in w3svc.Children)
         {
            if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
            {
               Console.Write(de.Name);
            }
         
         }
      
      }
      public static void Main()
      {
         GetWebsiteID("Default Web Site");
      }
   
   }

Resources

Internet Information Services SDK (MSDN)