Chris Crowe's Blog
Ramblings of an IIS MVP ( MVP Since 1997 )

Powered By IIS 7

Search my blog

Some of my readers



My Microsoft Certifications


Dec 15, 1998

Dec 20, 2000

Jan 31, 2001

Jul 22, 2002

Nov 1, 2004

My Microsoft MVP Awards




1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
mvp.support.microsoft.com

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.

 

posted on Wednesday, June 28, 2006 12:54 AM | Filed Under [ c# IIS IIS / Tools / Scripts ]

Comments


# re: c# - How to display the IP Address, TCP/IP Port, and Host Header values from IIS


Does not work
Posted by Dan on 10/27/2007 2:50 AM

# re: c# - How to display the IP Address, TCP/IP Port, and Host Header values from IIS


WHat does that mean Dan? Do you get an error message?, do you have a more detailed comment than "does not work?"
Posted by Chris Crowe on 10/27/2007 7:53 AM

# re: c# - How to display the IP Address, TCP/IP Port, and Host Header values from IIS


Lovely, but your assumption is that web ID is known at design time, not always the case.
Posted by John on 10/31/2007 10:01 AM

Post Comment


Title *
Name *
Email
Url
Comment *  
Please add 4 and 6 and type the answer here: