C# - Find website ID by name

ID: 167
Created: 2005-06-09
Updated: 2005-06-09


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");
    }

}

Link(s)

Internet Information Services SDK (MSDN)

Home | Notes Home | Copyright © 2002 - 2005 Kristofer Gäfvert