This code snippet demonstrates how you can enumerate IIS Website Properties using C#.

// ------------------------
// This requires the following using statement
using System.DirectoryServices;
// ------------------------

// We connect to our desired web site
 DirectoryEntry de = new DirectoryEntry("IIS://localhost/w3svc/1234567890");

// Be advised that some property bindings (e.g. HttpRedirect) can only be set on a child object (e.g. the Root)
// Consult with the Metabase Property Reference to see available properties and where they are configurable
// IIS 6.0 Reference Guide :
// http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cde669f1-5714-4159-af95-f334251c8cbd.mspx
// So you could bind it to the /Root instead to get a list of otherwise unavailable properties
//DirectoryEntry de = new DirectoryEntry("IIS://localhost/w3svc/1234567890/Root");

// We then loop through every available Property
foreach (string strProperty in de.Properties.PropertyNames)
{
  // Write out the property name
  Console.WriteLine(strProperty);

  // Fill a Property Value Collection with the values of the property we are going through
  PropertyValueCollection pvc = de.Properties[strProperty];

  // Then we go through every value in the collection
  foreach (object Value in pvc)
  {
    // And write them out to console
    Console.WriteLine("\t" + Value.ToString());
  }
}
Tagged with: