using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace EnumSites
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry entry = new DirectoryEntry("IIS://localhost/w3svc/1");
PropertyValueCollection pvc = entry.Properties["ServerBindings"];
foreach (object value in pvc)
{
// Format is IPAddress:Port:HostHeader
string[] Bits = value.ToString().Split(':');
string IPAddress = Bits[0];
string TCPIPPort = Bits[1];
string HostHeader = Bits[2];
Console.WriteLine("IP = {0}, Port = {1}, Header = {2}",
(IPAddress.Length == 0) ? "(All Unassigned)" : IPAddress,
TCPIPPort,
(HostHeader.Length == 0) ? "(No Host Header)" : HostHeader);
}
Console.Read();
}
}
}
This simple code will allow you to display the IP Address, TCP/IP Port, and Host Header values for the default web site. The default web site has an Instance ID of 1.
If you want to display for a different web site then change the "IIS://localhost/w3svc/1" to another instance. You can determine the instance ID from the log file name in the log entry properties dialog in the IIS manager.